[Numpy-discussion] speed of numpy.ndarray compared to Numeric.array
Benjamin Root
ben.root at ou.edu
Fri Jan 7 12:49:44 EST 2011
On Fri, Jan 7, 2011 at 9:58 AM, EMMEL Thomas <Thomas.EMMEL at 3ds.com> wrote:
> 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
> #Type "help", "copyright", "credits" or "license" for more information.
>
> 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
>
Don't know how much of an impact it would have, but those timeit statements
for array creation include the import process, which are going to be
different for each module and are probably not indicative of the speed of
array creation.
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110107/a4d55ce2/attachment.html>
More information about the NumPy-Discussion
mailing list