The implementation of pow()
in math.h
is 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()
from math.h
must 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.