# [Numpy-discussion] speed of numpy.ndarray compared to Numeric.array

EMMEL Thomas Thomas.EMMEL at 3ds.com
Fri Jan 7 10:58:04 EST 2011

```Hi,

There are some discussions on the speed of numpy compared to Numeric in this list, however I have a topic
I don't understand in detail, maybe someone can enlighten me...
I use python 2.6 on a SuSE installation and test this:

#Python 2.6 (r26:66714, Mar 30 2010, 00:29:28)
#[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2

import timeit

#creation of arrays and tuples (timeit number=1000000 by default)

timeit.Timer('a((1.,2.,3.))','from numpy import array as a').timeit()
#8.2061841487884521
timeit.Timer('a((1.,2.,3.))','from Numeric import array as a').timeit()
#9.6958281993865967
timeit.Timer('a((1.,2.,3.))','a=tuple').timeit()
#0.13814711570739746

#Result: tuples - of course - are much faster than arrays and numpy is a bit faster in creating arrays than Numeric

#working with arrays

timeit.Timer('d=x1-x2;sum(d*d)','from Numeric import array as a; x1=a((1.,2.,3.));x2=a((2.,4.,6.))').timeit()
#3.263314962387085
timeit.Timer('d=x1-x2;sum(d*d)','from numpy import array as a; x1=a((1.,2.,3.));x2=a((2.,4.,6.))').timeit()
#9.7236979007720947

#Result: Numeric is three times faster than numpy! Why?

#working with components:

timeit.Timer('d0=x1[0]-x2[0];d1=x1[1]-x2[1];d2=x1[2]-x2[2];d0*d0+d1*d1+d2*d2','a=tuple; x1=a((1.,2.,3.));x2=a((2.,4.,6.))').timeit()
#0.64785194396972656
timeit.Timer('d0=x1[0]-x2[0];d1=x1[1]-x2[1];d2=x1[2]-x2[2];d0*d0+d1*d1+d2*d2','from numpy import array as a; x1=a((1.,2.,3.));x2=a((2.,4.,6.))').timeit()
#3.4181499481201172
timeit.Timer('d0=x1[0]-x2[0];d1=x1[1]-x2[1];d2=x1[2]-x2[2];d0*d0+d1*d1+d2*d2','from Numeric import array as a; x1=a((1.,2.,3.));x2=a((2.,4.,6.))').timeit()
#0.97426199913024902

Result: tuples are again the fastest variant, Numeric is faster than numpy and both are faster than the variant above using the high-level functions!
Why?

For various reasons I need to use numpy in the future where I used Numeric before.
Is there any better solution in numpy I missed?

Kind regards and thanks in advance

Thomas

This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged.  If you are not one of the named recipients or have received this email in error, (i) you should not read, disclose, or copy it, (ii) please notify sender of your receipt by reply email and delete this email and all attachments, (iii) Dassault Systemes does not accept or assume any liability or responsibility for any use of or reliance on this email.For other languages, go to http://www.3ds.com/terms/email-disclaimer.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110107/13c86822/attachment.html>
```