Optimization ???

bearophile bearophileHUGS at lycos.com
Thu Feb 20 16:18:47 PST 2014


> I don't have time now to do the optimization for you now, sorry.

I have improved your code a little, but I don't know the
performance of this compared to the C# code:
http://dpaste.dzfl.pl/0dab53bf85ad

I compile and run it with ldc2 with:

ldmd2 -wi -O -release -inline -noboundscheck -run test.d

------------------------


A second version uses a struct:

struct Student {
     string name;
     Date birthday;
     int evaluation;

     string getState() {
         return name ~
                "'s birthday " ~
                birthday.toSimpleString ~
                " and his evaluation is " ~
                evaluation.text;
     }
}

void bench() {
     Student* michael;

     foreach (immutable count; 0 .. 1_000_000) {
         michael = new Student("Michael", Date(1998, 5, 1), 12);
         michael.name = "Joseph" ~ count.text;
     }
     michael.getState.writeln;
}


A third version allocates the struct on the stack:

void bench() {
     Student michael;

     foreach (immutable count; 0 .. 1_000_000) {
         michael = Student("Michael", Date(1998, 5, 1), 12);
         michael.name = "Joseph" ~ count.text;
     }

     michael.getState.writeln;
}


The timings I'm seeing:

Joseph999999's birthday 1998-May-01 and his evaluation is 12
Execution time : 651 ms

Joseph999999's birthday 1998-May-01 and his evaluation is 12
Execution time : 563 ms

Joseph999999's birthday 1998-May-01 and his evaluation is 12
Execution time: 440 ms.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list