Serving the Quantitative Finance Community

  • 1
  • 4
  • 5
  • 6
  • 7
  • 8
  • 11
 
User avatar
quantmeh
Posts: 0
Joined: April 6th, 2007, 1:39 pm

C++ virtual function cost

June 3rd, 2010, 10:11 am

QuoteOriginally posted by: CuchulainnNow I want to model animals whose bite(), suck(), strangle() is worse than their bark(). So, the class is "Dangerous_Animals", how do I do it?there are many approaches, such as multiple inheritance in C++ or CLU's 'where' clause. i personally like the latter, it's also very compatible with GP approach
 
User avatar
quantmeh
Posts: 0
Joined: April 6th, 2007, 1:39 pm

C++ virtual function cost

June 3rd, 2010, 10:12 am

QuoteOriginally posted by: renormWe might add dummy "bark" method to the fox class, but it defeats the purpose of OOP.why?
 
User avatar
quantmeh
Posts: 0
Joined: April 6th, 2007, 1:39 pm

C++ virtual function cost

June 3rd, 2010, 10:25 am

Java does this on the fly using monomorphic call transformation, i.e. inlining virtual functionQuants should think of algorithms and models. it's a waste of talent to think about virtues of "virtual destructors" and other programming crap. this should be left to programmers, they cant do anything else anyways
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 10:57 am

QuoteOriginally posted by: outrunHere is an example where derived is just as fast as normal. The compiler eliminates the vtable because all functions are virtual and pure (the Cuch code in the other thread has mixed virtual non-virtual functions)If it doesn't work with VS (it works with gcc) then you can add the _declspec(novtable) compiler switchI get the same for both indeed. But the test is using a derived class objectly _directly_. What about using the run time polymorphism? I tried this and standalone is slightly faster. btw vs2008 runs when (and otherwise not)
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, 11:44 am

A more realistic case for me is when we use multiple polymorphic function. Then we get 2 times and 8 times as slow in debug/release modes, respectively.I think the compiler has optimised your code.We cannot expect the compiler to know all this, so we have to be careful.
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, 12:47 pm

outrun,The code uses the same argument value for all calls. Try ptr_base->func((i&1)*(j&1)*3.14) vs ptr_standalone->func((i&1)*(j&1)*3.14). Or try do feed the output from mersenne twister to the functions. On my system polymorphic calls are about 2 times slower in release more.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ virtual function cost

June 3rd, 2010, 1:25 pm

QuoteMy point is that the hight cost of virtual functions is a false exaggerated claims, you just need to know what's going on and have a good compiler. Cost can be in the range of 0 to 15% I would say.That's a big statement! Are you saying I (and others) concocted these results? I repeat: VS2008 in release mode is 8 times slower.Just run your code in DEBUG/RELEASE modes (both)
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:25 pm

(double)
Last edited by Cuchulainn on June 2nd, 2010, 10:00 pm, edited 1 time in total.