Old problem with performance

Christopher Wright dhasenan at gmail.com
Sun Feb 8 10:02:55 PST 2009


Weed wrote:
> (Has started here:
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=81359)
> 
> To me still does not give rest performance of classes (in comparison
> with C++ or with D structs)

On my system, your struct example averaged 0.36 seconds, and your class 
example averaged 0.38 seconds.

Your benchmark is flawed in three ways:
1. You're timing allocations in the class example. Use opAddAssign to 
avoid it.
2. You're passing large structs in the struct example. Use ref to avoid it.
3. c1 is never assigned to, so c1 + c1 + c1 is moved outside the loop. 
The assignment has no side effect, so the loop is optimized out. Replace 
"c1 + c1 + c1" with "c1 + c2 + c1", for instance, and the struct example 
takes 20 seconds rather than 0.36. The original class example takes 57 
seconds to do 50 million allocations.

Using those tricks, the class example takes about four seconds on my 
machine, and the struct example takes two.

For reference, the benchmarks I used:
// class example -------------------------
scope class C {
     int i;
     real[5] unused; // to prevent returning this object in registers

     final void opAddAssign( C src ) {
         this.i += src.i;
     }
}

int main() {
     scope c1 = new C;
     scope c2 = new C;

     // initialise i by "random" value to prevent compile-time calculation
     c1.i = cast(int)&c1;
     c2.i = 0;

     for(int i = 0; i < 50_000_000; ++i)
     {
         c2 += c1;
         c2 += c1;
         c2 += c1;
     }

     return c2.i;
}

// struct example --------------------------
struct C {
     int i;
     real[5] unused; // to prevent returning this object in registers

     void opAddAssign( ref C src ) {
         this.i += src.i;
     }
}

int main() {
     C c1, c2;

     // initialise i by "random" value to prevent compile-time calculation
     c1.i = cast(int)&c1;
     c2.i = 0;

     for(int i = 0; i < 50_000_000; ++i)
     {
         c2 += c1;
         c2 += c1;
         c2 += c1;
     }

     return c2.i;
}



More information about the Digitalmars-d mailing list