Page 7 of 11

C++ virtual function cost

Posted: June 3rd, 2010, 1:45 pm
by Cuchulainn
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?

C++ virtual function cost

Posted: June 3rd, 2010, 1:56 pm
by Cuchulainn
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.

C++ virtual function cost

Posted: June 3rd, 2010, 2:07 pm
by Cuchulainn
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.

C++ virtual function cost

Posted: June 3rd, 2010, 2:14 pm
by Cuchulainn
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.

C++ virtual function cost

Posted: June 3rd, 2010, 2:24 pm
by renorm
The results with MinGW GCC 4.5 are similar to those posted by Cuchulainn.outrun,Are you sure that your test used polymorphic calls?

C++ virtual function cost

Posted: June 3rd, 2010, 2:26 pm
by Cuchulainn
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.

C++ virtual function cost

Posted: June 3rd, 2010, 3:51 pm
by AVt
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?

C++ virtual function cost

Posted: June 3rd, 2010, 4:05 pm
by Cuchulainn
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.

C++ virtual function cost

Posted: June 3rd, 2010, 6:55 pm
by Cuchulainn
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. "