"pow" (power) function

Terry Reedy tjreedy at udel.edu
Thu Mar 16 21:36:57 EST 2006


"Mike Ressler" <mike.ressler at alum.mit.edu> wrote in message 
news:1142529837.2880.12.camel at dhcp-78-140-229.jpl.nasa.gov...
> I have a counterexample. In the original timeit example, 111**111 was
> used. When I run that
>
>>>> timeit.Timer("pow(111,111)").timeit()
> 10.968398094177246
>>>> timeit.Timer("111**111").timeit()
> 10.04007887840271
>>>> timeit.Timer("111.**111.").timeit()
> 0.36576294898986816
>
> The pow and ** on integers take 10 seconds, but the float ** takes only
> 0.36 seconds. (The pow with floats takes ~ 0.7 seconds). Clearly
> typecasting to floats is coming in here somewhere. (Python 2.4.1 on
> Linux FC4.)

For floats, f**g == exp(log(f**g)) == exp(g*log(f)) (with maybe further 
algebraic manipulation, depending on the implementation).  The time for 
this should only be mildly dependent on the magnitudes of f and g.

The time for i**j, on the other hand, grows at least as fast as log(j).  So 
I should expect comparisons to depend on magnitudes, as you discovered.

Terry Jan Reedy







More information about the Python-list mailing list