A few days ago, I published a post comparing the
`performance of std::pow against direct multiplications `_. When not compiling with -ffast-math, direct multiplication was significantly faster than :code:`std::pow`, around two orders of magnitude faster when comparing :code:`x * x * x` and :code:`code:std::pow(x, 3)`.
One comment that I've got was to test for which :code:`n` is
:code:`code:std::pow(x, n)` becoming faster than multiplying in a loop. Since
std::pow is using a special algorithm to perform the computation rather than be
simply loop-based multiplications, there may be a point after which it's more interesting to use the
algorithm rather than a loop. So I decided to do the tests. You can also find
the result in the original article, which I've updated.
First, our pow function:
.. code:: c++
double my_pow(double x, size_t n){
double r = 1.0;
while(n > 0){
r *= x;
--n;
}
return r;
}
And now, let's see the performance. I've compiled my benchmark with GCC 4.9.3
and running on my old Sandy Bridge processor. Here are the results for 1000
calls to each functions:
.. raw:: html
We can see that between :code:`n=100` and :code:`n=110`, :code:`std::pow(x, n)`
starts to be faster than :code:`my_pow(x, n)`. At this point, you should only
use :code:`std::pow(x, n)`. Interestingly too, the time for :code:`std::pow(x,
n)` is decreasing. Let's see how is the performance with higher range of
:code:`n`:
.. raw:: html
We can see that the pow function time still remains stable while our loop-based
pow function still increases linearly. At :code:`n=1000`, :code:`std::pow` is
one order of magnitude faster than :code:`my_pow`.
Overall, if you do not care much about extreme accuracy, you may consider using
you own pow function for small-ish (integer) :code:`n` values. After
:code:`n=100`, it becomes more interesting to use :code:`std::pow`.
If you want more results on the subject, you take a look at the
`original article `_.
If you are interested in the code of this benchmark, it's available online:
`bench_pow_my_pow.cpp `_
.. raw:: html