On Sat, May 31, 2014 at 4:54 PM, Larry Evans
On 05/20/14 15:48, Zach Laine wrote:
I've seen function template vs. metafunction template instantiations profiled before, and I did it myself a few years ago. Function templates have been slower in every profile I've seen. In my profilings, they were ~20% slower.
The linked article appears to get better results not by using constexpr alone, but by using it to do numeric computations that allow it to reduce template instantiations.
AFAICT, the number of template function instantiations in the linked article are the same as the number of template class instantiations.
When I run the attached, 1st with:
defined(CONSTEXPR)
then with:
!defined(COnSTEXPR)
the same values appear in the:
(yesno,recurs)
output, indicating the same number of instantiations.
Am I missing something?
Yes. The two approaches are performing the same number of steps, but one is using template instantiations to do so and the other is not. Note the lack of the "template" keyword anywhere in your constexpr version. The compiler must do the same number of steps in both cases, but evaluating a constexpr function at compile time N times and instantiating N templates is not the same thing, and does not compile at the same speed; the template instantiations take longer. If you had instead implemented the constexpr version using size_t template parameters instead of function parameters, I'm pretty confident the constexpr version would compile more slowly. Zach