Speed of calculating powers (in python)

Basically naive multiplication is O(n) with a very low constant factor. Taking the power is O(log n) with a higher constant factor (There are special cases that need to be tested… fractional exponents, negative exponents, etc) . Edit: just to be clear, that’s O(n) where n is the exponent.

Of course the naive approach will be faster for small n, you’re only really implementing a small subset of exponential math so your constant factor is negligible.

Leave a Comment