1

I saw this old question about std::pow: What is more efficient? Using pow to square or just multiply it with itself?

The old question mentions std::pow has a variant that is std::pow(double, int) and some users said this version might be faster.

Is std::pow(double, int) faster than std::pow(double, double)? if yes, how much faster?

p.s. standard boilerplate disclaimer about "I already know that premature optimization is evil, but I'm asking this optimization question for academic purposes or I've already identified that line/block of code as a bottleneck"

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
  • 4
    FWIW, the overloads that took an in have been removed from C++ since C++11 – NathanOliver Apr 11 '18 at 18:39
  • Honestly, look at assembly and see. integer power should easily translate into vectorization. Benchmark it. – Michael Dorgan Apr 11 '18 at 18:40
  • 5
    "how much faster" is really hard to answer without the dataset and/or platform you wish to perform the operations on. In other words, you'd need to benchmark it – Justin Apr 11 '18 at 18:40
  • 8
  • 3
    It can be. The C++ library implementation that I use overloads it to a trivial for-loop that uses multiply-shift. YMMV. – Hans Passant Apr 11 '18 at 18:44
  • 4
    If you want to know which is faster, *measure it yourself*. https://ericlippert.com/2012/12/17/performance-rant/ – Jim Mischel Apr 11 '18 at 18:47
  • 1
    not sure what this question is aiming for, but with integer powers you can play funny tricks like `x^n = (x^n/2 * x^n/2)` for even `n` (and similar for odds) so you basically can reduce the number of multiplications required to something around log2(n). Not sure if there is anything similar for double powers. Anyhow afaik `pow` never was really meant to be used with integers – 463035818_is_not_an_ai Apr 11 '18 at 19:00
  • For large exponents I would expect the same, for small ones the `int` one might be faster. Hard to know for sure, since `pow` is actually tricky to implement maintaining the precision and libraries have probably very different implementations. – zch Apr 11 '18 at 19:00
  • 2
    @user463035818. This operation cannot be used in general on `double`s, but library might use it in easy cases (for example, when exponent is small integer) to give fast response. In general the formula `a**b = exp(log(a)*b)` would be used. – zch Apr 11 '18 at 19:10

1 Answers1

-1

Expectation:

std::pow(double, int) is the log-reducing for loop for log(n) runtime.

std::pow(double x, double y) { exp(log(x) * y); }

Should be pretty close.


from comments:

  • en.cppreference.com/w/cpp/numeric/math/pow (7) Since C++11, "If any argument has integral type, it is cast to double". – Bob__ Apr 11 '18 at 20:10
  • My interpretation (I may be wrong and library implementers may disagree) of that statement is that until C++11, they may have had different implementations, but since C++11, they should share the same, while the parameters are simply casted. – Bob__ Apr 11 '18 at 20:25
Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 1
    http://en.cppreference.com/w/cpp/numeric/math/pow (7) Since C++11, *"If any argument has integral type, it is cast to double"*. – Bob__ Apr 11 '18 at 20:10
  • @Bob__: I'm certain that's a result equivalency not the actual implementation. – Joshua Apr 11 '18 at 20:19
  • 1
    My interpretation (I may be wrong and library implementers may disagree) of that statement is that until C++11, they may have had different implementations, but since C++11, they should share the same, while the parameters are simply casted. – Bob__ Apr 11 '18 at 20:25