Re: "pow" (power) function
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: "pow" (power) function

From: Mike Ressler <mike.ressler@alum.mit.edu>
Date: Thu Mar 16 2006 - 18:23:57 CET

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
Received on Sun Apr 30 12:01:11 2006