Page 1 of 1
crtp vs virtual functions
Posted: February 23rd, 2012, 7:54 am
by ktang
What is crtp?Where is it used?What is the alternative?What is the advantage?
crtp vs virtual functions
Posted: February 23rd, 2012, 9:13 am
by Cuchulainn
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?
crtp vs virtual functions
Posted: February 23rd, 2012, 12:52 pm
by Polter
crtp vs virtual functions
Posted: June 1st, 2014, 9:58 am
by Cuchulainn
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.
crtp vs virtual functions
Posted: February 8th, 2015, 9:38 pm
by wejgomi
For a beginner level introduction , I`d suggest these links :
http://nativecoding.wordpress.com/2015/ ... -benchmark
crtp vs virtual functions
Posted: February 9th, 2015, 8:59 am
by Cuchulainn
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
crtp vs virtual functions
Posted: February 9th, 2015, 10:48 am
by Cuchulainn
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.
crtp vs virtual functions
Posted: February 9th, 2015, 10:54 am
by Cuchulainn
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.
crtp vs virtual functions
Posted: June 5th, 2015, 11:25 pm
by wejgomi
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 )