"pow" (power) function

Mike Ressler mike.ressler at alum.mit.edu
Thu Mar 16 12:23:57 EST 2006


On Wed, 2006-03-15 at 18:46 -0800, Ben Cartwright wrote:

> Anyway, if you want to see the int vs. float issue in action, try this:
> 
>   >>> from timeit import Timer
>   >>> Timer('2**2').timeit()
>   0.12681011582321844
>   >>> Timer('2.0**2.0').timeit()
>   0.33336011743438121
>   >>> Timer('2.0**2').timeit()
>   0.36681835556112219
>   >>> Timer('2**2.0').timeit()
>   0.37949818370600497
> 
> As you can see, the int version is much faster than the float version.

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.)

Mike





More information about the Python-list mailing list