I just was wondering how the pow function of the math.h library works, does it implement the simplest sequencial algorithm or does it use another one else?
I just know the repeated squaring algorithm, which reports O(log n), maybe this is the implemented algorithm by the pow function?
So I just made some tests using the sequential algorithm vs pow and found out that the first version is almost 3 times faster than the second. Does calling functions really punish that much the performance of this test? Why?
Any other comments explaining what's happening, or how pow is implemented are welcome.
EDIT: I was wrong, pow is 3 times faster than the sequential algorithm.
The implementation of
pow()inmath.his a lot more complex than that - take a look at this freely available implementation (link).A problem with repeated squaring is that it is not general enough to deal with fractional powers. The
pow()frommath.hmust deal with it, so it is necessarily slower on some of the test cases. However, since the repeated squaring function does not have the same functionality, the comparison is not apples-to-apples.Generally speaking, it is much easier to optimize for performance if you do not need to handle the general case. For example, if you never raise numbers to fractional powers, you could potentially make an algorithm that beats the library function 3:1 in a micro-benchmark. This should come with understanding that the applicability of the "faster" function is not as wide.