[Tutor] finding square roots

Alan Gauld alan.gauld at btinternet.com
Sat Oct 6 00:01:56 CEST 2007


"Eric Brunson" <brunson at brunson.com> wrote

> >>> import timeit
> >>> t = timeit.Timer( "12 ** .5" )
> >>> print t.timeit(10000000)
> 2.29147315025
> >>> t = timeit.Timer( "sqrt(12)", "from math import sqrt" )
> >>> print t.timeit(10000000)
> 7.17679214478
>
> Looks like ** is about three times faster than sqrt(), but that 
> could be
> the function overhead.

>>> timeit.Timer("import math; math.sqrt(64)").timeit(1000)
0.002298138362618829
>>> timeit.Timer("import math; 64 ** 0.5").timeit(1000)
0.018225722100503106

I get the opposite result by includsing the importt in both
timing sesions. Looks like it was the import that was slow.
But that shouldn't affect real world usage sinmce you only
do it once...

Alan G.




More information about the Tutor mailing list