Has anyone else ever wondered whether exp()
is quicker than the more general function pow()
? I conducted a rapid benchmark on JsPerf, and the results were quite intriguing. You can view the test here.
Math.exp(logBase * exponent); // performed the fastest
Math.exp(Math.log(base) * exponent); // in the middle
Math.pow(base, exponent); // the slowest
I understand that the outcomes can differ significantly depending on the system's architecture and programming language, but I am also curious about the theoretical aspect of it. Is pow(a, b)
essentially implemented as exp(log(a) * b)
, or is there a more sophisticated method to compute power "directly" (in languages such as C++, C#, or JavaScript)? Are there specific CPU instructions dedicated to exp, log, or pow on certain devices?
To my knowledge, both exp()
and log()
are calculated using Taylor series and are resource-intensive processes. This leads me to believe that for a constant base of power, the following code snippet:
double logBase = log(123.456);
for (int i = 0; i < 1024; ++i) {
exp(logBase * 654.321);
}
is more efficient than this one:
for (int i = 0; i < 1024; ++i) {
pow(123.456, 654.321);
}
Is my assumption correct?