In C++ Template Metaprogramming Appendix C "Compile Time Performance" Abraham and Gurtovoy messured how memoization of template instantiation effects compile time. The book was written in 2005 and I think memoization is better implemented today (I didn't measure). So the question to answer is which implementation can benefit more from memoization. I have edited the code a little bit Version 1 and Version 2. Now it will emit an error when reverse_impl
is instantiated, so we can simply count the errors. I reverse 2 lists list<int, short, char>
and list<short, char>
. Version 1 emits 4 errors and Version 2 emits 7 errors. In this particular case Version 1 benefits from memoization with these two lists (list2 is the tail of list1) and Version 2 doesn't. So I would expect Version 1 is faster.
So it would be best to implement algorithms in terms of other algorithms which are known to benefit from memoization and I think most of them use head recursion.