www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - performance of structs vs classes

reply =?ISO-8859-1?Q?Christian_K=F6stlin?= <christian.koestlin gmail.com> writes:
I have this small program here, which compares the call-overhead of 
methods from classes to structs. I guess this always has to be, because 
of the "polymorphism" of methods of objects.

Is there something I can do to improve on the performance of methods 
calls with classes?

please see the source here:
https://gist.github.com/1242911


thanks in advance

christian
Sep 26 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
You didn't call sw.reset() before calling sw.stop()!
Sep 26 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/26/11, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:
 You didn't call sw.reset() before calling sw.stop()!

Sep 26 2011
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
In the class test reset the timer:
    {
        auto h = new HClass();
        sw.reset(); // <-
        sw.start();
    }
Sep 26 2011
parent reply =?ISO-8859-1?Q?Christian_K=F6stlin?= <christian.koestlin gmail.com> writes:
On 09/26/2011 08:29 PM, Andrej Mitrovic wrote:
 In the class test reset the timer:
      {
          auto h = new HClass();
          sw.reset(); //<-
          sw.start();
      }

i compiled with: dmd -release -O -inline -m64 -oftarget/dmd/structs_vs_classes experimental/structs_vs_classes.d and gdc -O3 -inline -o target/gdc/structs_vs_classes experimental/structs_vs_classes.d wich resulted in: target/dmd/structs_vs_classes time with struct: 11326 for 1 time with class: 11323 for 1 and target/gdc/structs_vs_classes time with struct: 4011 for 1 time with class: 6880 for 1 which is much better than my wrong measurements... thanks for the answer!!! christian
Sep 26 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Christian K.:

 i totally misused the stopwatch api.

I have misused it the same way, the first time. If other people will find the same problem then it means its API has problems. Bye, bearophile
Sep 26 2011
parent =?UTF-8?B?Q2hyaXN0aWFuIEvDtnN0bGlu?= <christian.koestlin gmail.com> writes:
On 09/26/2011 10:30 PM, Steven Schveighoffer wrote:
 On Mon, 26 Sep 2011 15:58:48 -0400, bearophile
 <bearophileHUGS lycos.com> wrote:

 Christian K.:

 i totally misused the stopwatch api.

I have misused it the same way, the first time. If other people will find the same problem then it means its API has problems.

Have you ever used a real stopwatch? It works the same way :) Start/stop does not reset the time to 0, it just adds more time to the total. Reset clears to 0. I think probably the documentation needs to be better. The example also suggests start resets the stopwatch. -Steve

there should be no problem (i also used it correctly in another benchmark, but it was quite late and ...) thanks christian
Sep 26 2011
prev sibling parent =?UTF-8?B?Q2hyaXN0aWFuIEvDtnN0bGlu?= <christian.koestlin gmail.com> writes:
On 09/26/2011 10:34 PM, Steven Schveighoffer wrote:
 On Mon, 26 Sep 2011 15:36:04 -0400, Christian Köstlin
 <christian.koestlin gmail.com> wrote:

 On 09/26/2011 08:29 PM, Andrej Mitrovic wrote:
 In the class test reset the timer:
 {
 auto h = new HClass();
 sw.reset(); //<-
 sw.start();
 }

i compiled with: dmd -release -O -inline -m64 -oftarget/dmd/structs_vs_classes experimental/structs_vs_classes.d and gdc -O3 -inline -o target/gdc/structs_vs_classes experimental/structs_vs_classes.d wich resulted in: target/dmd/structs_vs_classes time with struct: 11326 for 1 time with class: 11323 for 1 and target/gdc/structs_vs_classes time with struct: 4011 for 1 time with class: 6880 for 1

use -frelease to disable invariant calls on gdc. Very important! -Steve

thanks! christian
Sep 26 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 26 Sep 2011 14:14:09 -0400, Christian K=C3=B6stlin  =

<christian.koestlin gmail.com> wrote:

 I have this small program here, which compares the call-overhead of  =

 methods from classes to structs. I guess this always has to be, becaus=

 of the "polymorphism" of methods of objects.

 Is there something I can do to improve on the performance of methods  =

 calls with classes?

 please see the source here:
 https://gist.github.com/1242911

In addition to Andrej's information (call sw.reset()), make *sure* you = = compile in -release mode, because in non -release mode, all object metho= ds = are followed by a call to Object's invariant, which is never inlined! = However, no invariants are called in release mode. -Steve
Sep 26 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 26 Sep 2011 15:58:48 -0400, bearophile <bearophileHUGS lycos.com>  
wrote:

 Christian K.:

 i totally misused the stopwatch api.

I have misused it the same way, the first time. If other people will find the same problem then it means its API has problems.

Have you ever used a real stopwatch? It works the same way :) Start/stop does not reset the time to 0, it just adds more time to the total. Reset clears to 0. I think probably the documentation needs to be better. The example also suggests start resets the stopwatch. -Steve
Sep 26 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 26 Sep 2011 15:36:04 -0400, Christian K=C3=B6stlin  =

<christian.koestlin gmail.com> wrote:

 On 09/26/2011 08:29 PM, Andrej Mitrovic wrote:
 In the class test reset the timer:
      {
          auto h =3D new HClass();
          sw.reset(); //<-
          sw.start();
      }

i compiled with: dmd -release -O -inline -m64 -oftarget/dmd/structs_vs_classes =

 experimental/structs_vs_classes.d
 and
    gdc -O3 -inline -o target/gdc/structs_vs_classes  =

 experimental/structs_vs_classes.d

 wich resulted in:
 target/dmd/structs_vs_classes
 time with struct: 11326 for 1
 time with class: 11323 for 1

 and
 target/gdc/structs_vs_classes
 time with struct: 4011 for 1
 time with class: 6880 for 1

use -frelease to disable invariant calls on gdc. Very important! -Steve
Sep 26 2011