# [SciPy-user] Speed differences in sqrt calculation: what is good to know

Johann Cohen-Tanugi cohen@slac.stanford....
Wed Jun 11 14:59:08 CDT 2008

```In [10]: rndvals=random.rand(10000)

In [11]: %timeit ssqrt(rndvals)
1000 loops, best of 3: 598 µs per loop

In [12]: %timeit nsqrt(rndvals)
1000 loops, best of 3: 362 µs per loop

JCT

Matthieu Brucher wrote:
> Hi,
>
> Don't forget that numpy's sqrt and scipy's sqrt are not optimized
> towards single computations.
> If you really want to compare them, time the square root of 10000
> elements.
>
> Matthieu
>
> 2008/6/11 Ivo Maljevic <ivo.maljevic@gmail.com
> <mailto:ivo.maljevic@gmail.com>>:
>
>     Based on comments from Gael Varoquaux and David Cournapeau , I did
>     the execution time test.
>     At least for me, it is clear that if the number is a real scalar,
>     AND the expected result is also real,
>     the best way is to call the math version of sqrt() function. The
>     differences are more than significant, as you can see:
>
>     In [3]: from math import sqrt as msqrt
>     In [4]: from numpy import sqrt as nsqrt
>     In [5]: from scipy import sqrt as ssqrt
>
>     In [6]: %timeit msqrt(3.14)
>     1000000 loops, best of 3: 479 ns per loop
>
>     In [7]: %timeit nsqrt(3.14)
>     100000 loops, best of 3: 10.8 µs per loop
>
>     In [8]: %timeit ssqrt(3.14)
>     10000 loops, best of 3: 74.5 µs per loop
>
>
>     _______________________________________________
>     SciPy-user mailing list
>     SciPy-user@scipy.org <mailto:SciPy-user@scipy.org>
>     http://projects.scipy.org/mailman/listinfo/scipy-user
>
>
>
>
> --
> French PhD student
> Website : http://matthieu-brucher.developpez.com/
> Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92