On 7/20/07, Kevin Jacobs <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
wrote:

On 7/20/07, Kevin Jacobs <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
wrote:
>
> On 7/20/07, Charles R Harris < [EMAIL PROTECTED]> wrote:
> >
> > I expect using sqrt(x) will be faster than x**.5.
> >
>
> I did test this at one point and was also surprised that sqrt(x) seemed
> slower than **.5.  However I found out otherwise while preparing a timeit
> script to demonstrate this observation.  Unfortunately, I didn't save the
> precise script I used to explore this issue the first time.  On my system
> for arrays with more than 2 elements, sqrt is indeed faster.  For smaller
> arrays, the different is negligible, but inches out in favor of ** 0.5.
>


This is just not my day.  My observations above are valid for integer
arrays, but not float arrays:

sqrt(int array)   :  6.98 usec/pass
(int array)**0.5  : 22.75 usec/pass
sqrt(float array) :  6.70 usec/pass
(float array)**0.5:  4.66 usec/pass



From the source, it appears that powers [-1, 0, 0.5, 1, 2] are optimized for
float and complex types, while one power, 2, is optimized for other types. I
can't recall why that is however.


Generated by:

import timeit

n=100000

t=timeit.Timer(stmt='sqrt(arange(3))',setup='from numpy import
arange,array,sqrt\nx=arange(100)')
print 'sqrt(int array)   : %5.2f usec/pass' % (1000000*t.timeit
(number=n)/n)

t=timeit.Timer(stmt='x**0.5',setup='from numpy import
arange,array\nx=arange(100)')
print '(int array)** 0.5  : %5.2f usec/pass' % (1000000*t.timeit
(number=n)/n)

t=timeit.Timer(stmt='sqrt(arange(3))',setup='from numpy import
arange,array,sqrt\nx=arange(100,dtype=float)')
print 'sqrt(float array) : %5.2f usec/pass' % (1000000* t.timeit
(number=n)/n)

t=timeit.Timer(stmt='x**0.5',setup='from numpy import
arange,array\nx=arange(100,dtype=float)')
print '(float array)**0.5: %5.2f usec/pass' % (1000000*t.timeit(number=n)/n)


-Kevin



_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion




--
.  __
.   |-\
.
.  [EMAIL PROTECTED]
_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to