Serving the Quantitative Finance Community

 
User avatar
ktang
Topic Author
Posts: 0
Joined: January 15th, 2010, 7:16 pm

crtp vs virtual functions

February 23rd, 2012, 7:54 am

What is crtp?Where is it used?What is the alternative?What is the advantage?
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

crtp vs virtual functions

February 23rd, 2012, 9:13 am

CRTP ~ static polymorphism.It is used a lot in Boost (e.g. Statecharts, Units, Iterator, Assignment) and Microsoft ATL.Used for performance, mainly. Also code compactness and reliability.Alternative is 1) OOP classic 2) Boost Function to emulate polymorphism.One poster here claims 10-fold improvement compared to PVMF in an app. Any specific code you want to optimise?
Last edited by Cuchulainn on February 22nd, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
Polter
Posts: 1
Joined: April 29th, 2008, 4:55 pm

crtp vs virtual functions

February 23rd, 2012, 12:52 pm

Last edited by Polter on February 22nd, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

crtp vs virtual functions

June 1st, 2014, 9:58 am

In a 2d ADE pde test we need to model pde coefficient. One test:1. std::function 160 seconds2. Namespace + C function pointers 103 seconds 3. CRTP == 2 + class structure, probably optimal.
Last edited by Cuchulainn on May 31st, 2014, 10:00 pm, edited 1 time in total.
 
User avatar
wejgomi
Posts: 0
Joined: February 7th, 2015, 10:49 pm

crtp vs virtual functions

February 8th, 2015, 9:38 pm

For a beginner level introduction , I`d suggest these links :http://nativecoding.wordpress.com/2015/ ... -benchmark
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

crtp vs virtual functions

February 9th, 2015, 8:59 am

Welcome to Wilmott.A while ago we did tests and found CRTP was 8-12 faster hereI have a few questions:1. When you say inlining with CRTP, do you mean in Base, Derived or both? In general, inline in Derived will not always be achievable?2. Instead of dynamic or static inheritance, what about plan to create ONE class with embedded function wrapper?class C{std::function<int (int)> tickDelegate;}Nice layout :-)
Last edited by Cuchulainn on February 8th, 2015, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

crtp vs virtual functions

February 9th, 2015, 10:48 am

QuoteWe see that CRTP implementation is 127 times faster than virtual methods implementationSo, there is a solution that is 254 times faster?It should be possible to take OP's code and show this claim.
Last edited by Cuchulainn on February 8th, 2015, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

crtp vs virtual functions

February 9th, 2015, 10:54 am

QuoteOriginally posted by: outrunan alternative is to use strong types and non-member functions acting on them. I find it more expressive, the design is better to maintain (non-member functions improve encapsulation: see Scott Meyers, Andrei Alexandrescu), and it turns out that it's faster than CRTP (because you don't need to do pointer indirection and type casting).doube d1 = price(my_swaption);doube d2 = price(my_callable_bond);We did some test here where we focused on finding a method to store all types in a heterogenous container without using CRTP (we gave each type it's own container and grouped the containers). That resulted in code that was twice as fast than CRTP.AFAIR, my recollection, the problem was slightly different; it was virtual functions versus non-member functions.But maybe it was CRTP... correct me if it was not so. ===And praise where praise is due: OP produced a nice report.
Last edited by Cuchulainn on February 8th, 2015, 11:00 pm, edited 1 time in total.
 
User avatar
wejgomi
Posts: 0
Joined: February 7th, 2015, 10:49 pm

crtp vs virtual functions

June 5th, 2015, 11:25 pm

QuoteOriginally posted by: CuchulainnWelcome to Wilmott.A while ago we did tests and found CRTP was 8-12 faster hereI have a few questions:1. When you say inlining with CRTP, do you mean in Base, Derived or both? In general, inline in Derived will not always be achievable?2. Instead of dynamic or static inheritance, what about plan to create ONE class with embedded function wrapper?class C{std::function<int (int)> tickDelegate;}Nice layout :-)Sorry for extremely late reply.I have been re-looking at it. Seperated CRTP and virtual methods source files , to be able to look at assembly outputs seperately.First thing is , I decreased number of iterations and I got a different result. With O3 and inlining , CRTP was only 6.5ish faster than virtual methods.As also looked at asm output, the indirection in virtual method is obvious , but the more eye-catching part was seeing SIMD instructions in CRTP. ( paddd )Here is updated blog page : https://nativecoding.wordpress.com/2015 ... mark-2/And here is the source codes with assembly outputs : https://github.com/akhin/benchmarks/tre ... pRegarding inling, to my understanding at least in the debugger , base class is inlined into derived method , definitely no indirectionAs for the second one, my purpose was just seeing CRTP against virtual methods since it still allows me to use a base pointer to refer to different types , without changing existing code too much.Honestly I am not aware of that method. ( One big class )