www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Profiling Inheritance

I am trying to profile virtual functions vs delegates

http://dpaste.dzfl.pl/dd10c841

   Num          Tree        Func        Per
   Calls        Time        Time        Call

       3     1019328      456148      152049     int 
main._testfooA(main.A)
       3     1809331      398939      132979     int 
main.testfooA(main.A)
       3      446842      382742      127580     int 
main.testfooB(main.B)


_testfooA is called when the delegate is null(this results in 
comparison that fails)

testfooA is called when the delegate is not null(which results in 
a comparison that passes and the delegate is called).

testfooB is simple virtual function calls.


What is Tree Time and how does it relate to performance? (call 
time is obvious)

Tree time is drastically different but the time per call is not 
(152 - 128)/128 ~= 20% slower.

I was interested in using the interface final pattern to provide 
default behavior but it seems slower than virtual calls. I was 
hoping that the null delegate test would be faster but I guess 
the branching thrashes the cache?


       1         176         176         176     int main.testfoo()

If I avoid virtual functions all together then the speed 
differences is immense, but it is over 800x slower. Not sure if 
the compiler is somehow optimizing out testfoo or if virtual 
function calls are really that slow. I do realize that the 
virtual functions and delegates are not doing much but it's still 
quite significant.
Aug 13 2013