July 24, 2008
On Mon, 21 Jul 2008 11:51:23 +0000, Luis P. Mendes wrote:

> Hi,
> 
> I use to program in Python, but I need some programs to run much faster.
> So, D seems to be as the best programming language for my needs.
> 
> Still, there's a long way to go because I've never programmed in C.
> 
> To begin with, is version 2 just a developer version or should I start by using it?
> 
> In Python, lists can have variables, functions, text and others as elements.  As far as I can recall from reading a C book, arrays in C don't have this possibility.  What about it in D?
> 
> And regarding list comprehensions like li = [elem*2 for elem in li]?  is there something close in D?
> 
> For example, how could I do something like: valores = []
> for c in lista_campos: valores.append(getattr(self,c)), so that I can
> have all the
> 
> Is there also any construct similar to dictionaries?
> 
> Are there D libraries to access PostgreSQL database or do I have to use C's?
> 
> I don't know about the theoretical issues regarding language development, so documentation with good examples is a must have. Is there something like www.diveintopython.org in the short term horizon?
> 
> These are just some topics I need to know about D.  I'd appreciate some answers.
> 
> 
> Luis

I don't have a lot to add to what was already said, but I do want to note that in using D you are likely going to lean quite a bit of C. This might be directly (something is explain using C), or indirectly (D constructs are similar to C's)

I don't have much experience with speed performance tackling, but have read many discussions. And one thing that is not pointed out is that DMD has not had huge development done in the area of optimizing code, and there is GDC that will let you use the GCC backend to do more optimization which supposedly is better than DMD, but still not the same as native C. And Hashtables are currently very inefficient compared to Python's.

On a note of when bearophile said that if you write code as you do in Python, you only get about Python speed. I'm not sure how close to the "Python" construct he is referring, but through my lack of experience with speed comparisons, if you right code that follows the D "style" you should see good performance. Hopefully bearophile or someone else will correct me if this is off base.

I would definitely suggest sticking with D1, yes even for Linux.

Final thoughts, D is a very clean language with a few hiccups and provides a range of tools. It may not suit your needs, but at the current time the best way to know is to try it yourself. If do use it, whether a success or failure, a report on how it went is always useful. Hope you add yourself to the community, and if not for this task, for your own joy.
July 24, 2008
Jesse Phillips wrote:
> On Mon, 21 Jul 2008 11:51:23 +0000, Luis P. Mendes wrote:
> 
>> Hi,
>>
>> I use to program in Python, but I need some programs to run much faster.
>> So, D seems to be as the best programming language for my needs.
>>  Still, there's a long way to go because I've never programmed in C.
>>
>> To begin with, is version 2 just a developer version or should I start
>> by using it?
>>
>> In Python, lists can have variables, functions, text and others as
>> elements.  As far as I can recall from reading a C book, arrays in C
>> don't have this possibility.  What about it in D?
>>
>> And regarding list comprehensions like li = [elem*2 for elem in li]?  is
>> there something close in D?
>>
>> For example, how could I do something like: valores = []
>> for c in lista_campos: valores.append(getattr(self,c)), so that I can
>> have all the
>>
>> Is there also any construct similar to dictionaries?
>>
>> Are there D libraries to access PostgreSQL database or do I have to use
>> C's?
>>
>> I don't know about the theoretical issues regarding language
>> development, so documentation with good examples is a must have. Is
>> there something like www.diveintopython.org in the short term horizon?
>>
>> These are just some topics I need to know about D.  I'd appreciate some
>> answers.
>>
>>
>> Luis
> 
> I don't have a lot to add to what was already said, but I do want to note that in using D you are likely going to lean quite a bit of C. This might be directly (something is explain using C), or indirectly (D constructs are similar to C's)
> 
> I don't have much experience with speed performance tackling, but have read many discussions. And one thing that is not pointed out is that DMD has not had huge development done in the area of optimizing code, and there is GDC that will let you use the GCC backend to do more optimization which supposedly is better than DMD, but still not the same as native C. And Hashtables are currently very inefficient compared to Python's.
> 
> On a note of when bearophile said that if you write code as you do in Python, you only get about Python speed. I'm not sure how close to the "Python" construct he is referring, but through my lack of experience with speed comparisons, if you right code that follows the D "style" you should see good performance. Hopefully bearophile or someone else will correct me if this is off base.
> 
> I would definitely suggest sticking with D1, yes even for Linux.
> 
> Final thoughts, D is a very clean language with a few hiccups and provides a range of tools. It may not suit your needs, but at the current time the best way to know is to try it yourself. If do use it, whether a success or failure, a report on how it went is always useful. Hope you add yourself to the community, and if not for this task, for your own joy.

Some very good points.  Cheers!

-Joel
July 24, 2008
Jesse Phillips:
> On a note of when bearophile said that if you write code as you do in Python, you only get about Python speed. I'm not sure how close to the "Python" construct he is referring, but through my lack of experience with speed comparisons, if you right code that follows the D "style" you should see good performance. Hopefully bearophile or someone else will correct me if this is off base.

From my experience if you write code in Python style (using my libs, mostly) you often have code that is about 1.5-4 times faster than the Python one.
As you say, if you program in a "D" style the speed can be quite higher.
Finally if you program in C style, the speed is generally only 1-10 times of the C, on average about 2 time slower in many small programs I have written :-)

Here you can find a discussion about a similar topic, in the zip you can find three D versions that show you what I am talking about (but here the D C-style version is about as fast as the D D-style version):
http://leonardo-m.livejournal.com/66171.html

Bye,
bearophile
July 24, 2008
"Luis P. Mendes" <luislupeXXXX@gmailXXXX.com> wrote in message news:g62lca$1c7p$1@digitalmars.com...
> Mon, 21 Jul 2008 13:10:54 -0400, bearophile wrote:
>
>>>I wanted to know if I can load values for attributes in a concise manner.<
>>
>> Can you show a working Python example of what you mean? So I/we can avoid guessing many times.
> Sure, for example:
>
> self.lista_campos:  ['indice', 'emac_periodos', 'emal_bool', 'emal_periodos', 'tr1_bool', 'tr1_periodos', 'tr1_sinal', 'tr1_percent', 'tr2_bool', 'tr2_periodos', 'tr2_sinal', 'tr2_percent', 'tr2_vezes']
>
> individuo_sql:  (2264, 42, True, 88, True, 15, '>', Decimal("0.49"),
> False, 6, '<', Decimal("0.84"), 4]
>
> for atributo, valor in zip(self.lista_campos, individuo_sql): setattr
> (self, atributo, valor)
>
> so that:
> print self.indice
> 2264
>
> print self.emac_periodos
> 42
>
> and so on.
>
> In this one I also used a zip function to pack values from two lists together.
>

IMO, that approach is very bad style since it makes it easy to accidentally misalign the elements of the arrays. That makes it difficult to keep the arrays synchronized and can cause hidden bugs.

Here are a few alternate approaches that I use in D (I would choose one of these based on what I was trying to do with the data. Also, note I haven't tried to compile these, so there may be some typos and such. Consider them semi-psuedo-code. Also, they might look much more readable in a fixed-width font.)

Methods 1-3 force the values to be strings, which might be acceptable if you're not going to do any further processing on them aside from just sticking them into SQL statements.

Methods 4-6 retain the correct types for the values and also give you varying levels of protection against accidentally misspelling the names or using names that are unaccounted for elsewhere (at the cost of not being able to create new names/fields at runtime - which might not be a problem depending on what you're doing).

// Method 1: Array of structs that each contain two strings
struct Pair
{
    char[] name;
    char[] value;
}

Pair[] pairs = [Pair("indice",    "2264"),
                Pair("emal_bool", "True"),
                Pair("tr1_sinal", ">"   )];
Stdout.formatln("Name 0:  {}", pairs[0].name);
Stdout.formatln("Value 0: {}", pairs[0].value);

// Method 2: Array of "length-2-arrays" of strings
enum Pair
{
    Name=0, Value=1;
}

char[][][] pairs = [["indice",    "2264"],
                    ["emal_bool", "True"],
                    ["tr1_sinal", ">"   ]];
Stdout.formatln("Name 0:  {}", pairs[0][Pair.Name]);
Stdout.formatln("Value 0: {}", pairs[0][Pair.Value]);

// Method 3: Associative array of strings
char[char[]] pairs;
pairs["indice"]    = "2264";
pairs["emal_bool"] = "True";
pairs["tr1_sinal"] = ">";
Stdout.formatln("Name:  indice");
Stdout.formatln("Value: {}", pairs["indice"]);

// Method 4: Struct
struct MyData
{
    int indice;
    bool emal_bool;
    char[] tr1_sinal;
}

// Not sure if "data" needs to be static
MyData data = {indice:    2264,
               emal_bool: true,
               tr1_sinal: ">"};
Stdout.formatln("Name:  indice");
Stdout.formatln("Value: {}", data.indice);

// Method 5: Class, without using constructor
class MyData
{
    int indice;
    bool emal_bool;
    char[] tr1_sinal;
}

auto data = new MyData();
data.indice    = 2264;
data.emal_bool = true;
data.tr1_sinal = ">";
Stdout.formatln("Name:  indice");
Stdout.formatln("Value: {}", data.indice);

// Method 6: Class, using constructor
class MyData
{
    int indice;
    bool emal_bool;
    char[] tr1_sinal;

    this(int indice, bool emal_bool, char[] tr1_sinal)
    {
        //I have a template mixin that can clean this up somewhat:
        this.indice    = indice;
        this.emal_bool = emal_bool;
        this.tr1_sinal = tr1_sinal;
    }
}

auto data = new MyData(2264, true, ">");
Stdout.formatln("Name:  indice");
Stdout.formatln("Value: {}", data.indice);


July 24, 2008
"Nick Sabalausky" <a@a.a> wrote in message news:g6arnr$b44$1@digitalmars.com...
>
> // Method 3: Associative array of strings
> char[char[]] pairs;

Sorry, that should be:

char[][char[]] pairs;


1 2 3
Next ›   Last »