Performance: function templates or regular functions

By on August 14, 2012

Due to some cleanup in the grid handling code, I wondered what would be faster, in terms of performance, between function templates and regular functions.

The numbers speak for their self. Function templates are way faster in this particular case.

ReleaseCand mode:
template_time duration: 0.236013
regular_time duration:  0.52903

The semantics between those two tests are identical, where one is using regular functions and the other function templates. The tests iterate through 30 million random vectors (but identical in both tests).

The method signatures look like this:

// Function template
template<size_t XS, size_t YS, size_t ZS>
MONSTERINLINE GridPosition toGridDim( const GlobalCoords &v );

// Regular function
MONSTERINLINE GridPosition toGridDim( const GlobalCoords &v, 
                                      const Dimension &dimension );

The size of the grid is a compile time constant, and the compiler has the chance to optimize the regular method the same way as the function template. I haven’t tested this on Linux with gcc/g++, so it might be quirk with the VS2010 C++ compiler.

In the real world there is no difference, given the following time per invocation:

latex path not specified.:   \large { \frac{0.52903}{30000000} = 1,76e-8 = 17.6 ns. }
latex path not specified.:   \large { \frac{0.236013}{30000000} = 7,87e-9 = 7.87 ns. }

No one will notice the difference between 7 nanoseconds or 17 nanoseconds.

One nanosecond is mighty short: https://en.wikipedia.org/wiki/Nanosecond

Posted in: coding
Tagged: , , ,

Comments

Be the first to comment.

Leave a Reply