Serving the Quantitative Finance Community

  • 1
  • 5
  • 6
  • 7
  • 8
  • 9
  • 11
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 1:45 pm

Here is the output in release mode in VS2008. Look the numbers for yourself.So, maybe gcc does not this problem, but does anyone know why VS2008 gives such discrepencies?
Last edited by Cuchulainn on June 2nd, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 1:56 pm

Could do, but it will not help to answer the question. And seeing that VS2008 is a major compiler it is now to find out why this is so. if you can catch the the short-lived arbitrage opportunity that your boss wants you to catch. This would be impossible in my current situation.
Last edited by Cuchulainn on June 2nd, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 2:07 pm

Quotethe VS2008 discrepancies are strange.. But well-known on this forum, as many posters have experienced it... Anyhows, here's the original code sample and even without nested polymorphism the V solution is 3 times slower (I get times of 7.2, 7.2, 2.4, 2.4, 2.5). Try it yourself on VS2008 and jay or nay.
Last edited by Cuchulainn on June 2nd, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 2:14 pm

QuoteOriginally posted by: outrunWe should look at generted intermediate assemblerYou're kidding. Quotewhy debug mode?? I have 100's of switches... This is about optimization isn't it?The reason being that there is no difference in speed between V and NV mode. Maybe this is what you are using.
Last edited by Cuchulainn on June 2nd, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
renorm
Topic Author
Posts: 1
Joined: February 11th, 2010, 10:20 pm

C++ virtual function cost

June 3rd, 2010, 2:24 pm

The results with MinGW GCC 4.5 are similar to those posted by Cuchulainn.outrun,Are you sure that your test used polymorphic calls?
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 2:26 pm

Here is a previous post by Luigi (QL) on another thread:QuoteOn what compiler, and what optimization level?On my Mac OS X machine:gcc 3.3, no optimizations:Non-virtual: 25.81Virtual: 54.91gcc 3.3, full optimization:Non-virtual: 2.02Virtual: 22.88However, my compiler is a bit old. They might have got better at optimizing virtual functions. I'll be able to try gcc 4 when I get back to my office (in a week.)You might try cranking up your optimization level, though.LuigiP.S. You have the virtual function at a disadvantage. In the linetemp = myObject -> funcV();the return value has to be converted from double to int to be assigned to temp. You might want to declare a double temp2 to assign to (that's what I did) or change the virtual function so that it returns an int.
Last edited by Cuchulainn on June 2nd, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
AVt
Posts: 90
Joined: December 29th, 2001, 8:23 pm

C++ virtual function cost

June 3rd, 2010, 3:51 pm

Just a trivial question by a OO-ignorant lurker:In the above code there is some "int max=10000000000;" which is beyondINT_MAX = 2^31 - 1 of usual C, it becomes 1410065408, i.e. modulo 2^31.To what is it reduced in your likewise settings?
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 4:05 pm

QuoteOriginally posted by: AVtJust a trivial question by a OO-ignorant lurker:In the above code there is some "int max=10000000000;" which is beyondINT_MAX = 2^31 - 1 of usual C, it becomes 1410065408, i.e. modulo 2^31.To what is it reduced in your likewise settings?AVt,You are right. Started with small max and then I increased it..I changed to long and somewhat less iterations 10^8. The virtual version is still 3 times slower.
Last edited by Cuchulainn on June 2nd, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 6:55 pm

It's even worse than I feared...I tried the same as my last code with OpenMP parallel for loopsFor virtual: 20% slower than 1 threadNon virtual: 250% slower than 1 threadThis was a simple, random code and I have not even got to the stage of testing for silent data corruption. On the other hand, when I make 'tmp' private to each thread (by default it is shared) then I do get linear speedup." I am putting myself to the fullest possible use, which is all I think that any conscious entity can ever hope to do. "
Last edited by Cuchulainn on June 2nd, 2010, 10:00 pm, edited 1 time in total.