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: Terry Reedy <tjreedy@udel.edu>
Date: Fri Mar 17 2006 - 03:36:57 CET

"Mike Ressler" <mike.ressler@alum.mit.edu> wrote in message
news:1142529837.2880.12.camel@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
Received on Sun Apr 30 12:03:15 2006