[Numpy-discussion] strange sin/cos performance

Jochen cycomanic at gmail.com
Tue Aug 4 21:18:33 EDT 2009


Hi all,
I see something similar on my system. 
OK I've just done a test. System is Ubuntu 9.04 AMD64
there seems to be a regression for float32 with high values:

In [47]: a=np.random.rand(10000).astype(np.float32)

In [48]: b=np.random.rand(10000).astype(np.float64)

In [49]: c=1000*np.random.rand(10000).astype(np.float32)

In [50]: d=1000*np.random.rand(1000).astype(np.float64)

In [51]: %timeit -n 10 np.sin(a)
10 loops, best of 3: 251 µs per loop

In [52]: %timeit -n 10 np.sin(b)
10 loops, best of 3: 395 µs per loop

In [53]: %timeit -n 10 np.sin(c)
10 loops, best of 3: 5.65 ms per loop

In [54]: %timeit -n 10 np.sin(d)
10 loops, best of 3: 87.7 µs per loop

In [55]: %timeit -n 10 np.sin(c.astype(np.float64)).astype(np.float32)
10 loops, best of 3: 891 µs per loop


Cheers
Jochen


On Mon, 3 Aug 2009 15:45:56 +0200
Emmanuelle Gouillart <emmanuelle.gouillart at normalesup.org> wrote:

> 	Hi Andrew,
> 
> 	%timeit is an Ipython magic command that uses the timeit
> module, see
> http://ipython.scipy.org/doc/stable/html/interactive/reference.html?highlight=timeit
> for more information about how to use it. So you were right to suppose
> that it is not a "normal Python". 
> 
> 	However, I was not able to reproduce your observations.
> 
> >>> import numpy as np
> >>> a = np.arange(0.0, 1000, (2 * 3.14159) / 1000, dtype=np.float32)
> >>> b = np.arange(0.0, 1000, (2 * 3.14159) / 1000, dtype=np.float64)
> >>> %timeit -n 10 np.sin(a)
> 10 loops, best of 3: 8.67 ms per loop
> >>> %timeit -n 10 np.sin(b)
> 10 loops, best of 3: 9.29 ms per loop
> 
> 	Emmanuelle
> 
> On Mon, Aug 03, 2009 at 09:32:57AM -0400, Andrew Friedley wrote:
> > While working on GSoC stuff I came across this weird performance  
> > behavior for sine and cosine -- using float32 is way slower than  
> > float64.  On a 2ghz opteron:
> >
> > sin float32 1.12447786331
> > sin float64 0.133481025696
> > cos float32 1.14155912399
> > cos float64 0.131420135498
> >
> > The times are in seconds, and are best of three runs of ten
> > iterations of numpy.{sin,cos} over a 1000-element array (script
> > attached).  I've produced similar results on a PS3 system also.
> > The opteron is running Python 2.6.1 and NumPy 1.3.0, while the PS3
> > has Python 2.5.1 and NumPy 1.1.1.
> >
> > I haven't jumped into the code yet, but does anyone know why
> > sin/cos are ~8.5x slower for 32-bit floats compared to 64-bit
> > doubles?
> >
> > Side question:  I see people in emails writing things like 'timeit  
> > foo(x)' and having it run some sort of standard benchmark, how
> > exactly do I do that?  Is that some environment other than a normal
> > Python?
> >
> > Thanks,
> >
> > Andrew
> 
> > import timeit
> 
> > t = timeit.Timer("numpy.sin(a)",
> >                 "import numpy\n"
> >                 "a = numpy.arange(0.0, 1000, (2 * 3.14159) / 1000,
> > dtype=numpy.float32)") print "sin float32", min(t.repeat(3, 10))
> 
> > t = timeit.Timer("numpy.sin(a)",
> >                 "import numpy\n"
> >                 "a = numpy.arange(0.0, 1000, (2 * 3.14159) / 1000,
> > dtype=numpy.float64)") print "sin float64", min(t.repeat(3, 10))
> 
> > t = timeit.Timer("numpy.cos(a)",
> >                 "import numpy\n"
> >                 "a = numpy.arange(0.0, 1000, (2 * 3.14159) / 1000,
> > dtype=numpy.float32)") print "cos float32", min(t.repeat(3, 10))
> 
> > t = timeit.Timer("numpy.cos(a)",
> >                 "import numpy\n"
> >                 "a = numpy.arange(0.0, 1000, (2 * 3.14159) / 1000,
> > dtype=numpy.float64)") print "cos float64", min(t.repeat(3, 10))
> 
> 
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list