[Numpy-discussion] Numpy array performance issue

Bruno Santos bacmsantos at gmail.com
Wed Feb 24 11:40:09 EST 2010


Funny. Which version of python are you using? My python is still better for
small lists. But you are rigth it gets better with size, here how the same
code performs on mine computer:
In [1]: N = 100

In [2]: import numpy as np

In [3]:  A = np.random.randint(0, 21, N)
   ...:

In [4]:  L = A.tolist()
   ...:

In [5]: %timeit len([e for e in L if e >= 10])
100000 loops, best of 3: 6.12 us per loop

In [6]: %timeit (A >= 10).sum()
100000 loops, best of 3: 6.34 us per loop

In [7]: N = 10000

In [8]: %macro mm 3 4 5 6
Macro `mm` created. To execute, type its name (without quotes).
Macro contents:
 A = np.random.randint(0, 21, N)
 L = A.tolist()
_ip.magic("timeit len([e for e in L if e >= 10])")
_ip.magic("timeit (A >= 10).sum()")


In [9]: mm
------> mm()
1000 loops, best of 3: 544 us per loop
10000 loops, best of 3: 98.3 us per loop

Anyway, thank you very much for your help I will try to change my code to
replace my for loop. I might need to come back to the mailing list if a run
into problems in the future.

All the best,
Bruno

2010/2/24 Robert Kern <robert.kern at gmail.com>

> On Wed, Feb 24, 2010 at 10:21, Bruno Santos <bacmsantos at gmail.com> wrote:
>
> >> The idiomatic way of doing this for numpy arrays would be:
> >>
> >> def test2(arrx):
> >>    return (arrx >= 10).sum()
> >>
> >  Even this versions takes more time to run than my original python
> version
> > with arrays.
>
> Works fine for me, and gets better as the size increases:
>
> In [1]: N = 100
>
> In [2]: import numpy as np
>
> In [3]: A = np.random.randint(0, 21, N)
>
> In [4]: L = A.tolist()
>
> In [5]: %timeit len([e for e in L if e >= 10])
> 100000 loops, best of 3: 15 us per loop
>
> In [6]: %timeit (A >= 10).sum()
> 100000 loops, best of 3: 12.7 us per loop
>
> In [7]: N = 1000
>
> In [8]: %macro mm 3 4 5 6
> Macro `mm` created. To execute, type its name (without quotes).
> Macro contents:
> A = np.random.randint(0, 21, N)
> L = A.tolist()
> _ip.magic("timeit len([e for e in L if e >= 10])")
> _ip.magic("timeit (A >= 10).sum()")
>
>
> In [9]: mm
> ------> mm()
> 10000 loops, best of 3: 103 us per loop
> 100000 loops, best of 3: 17.6 us per loop
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>  -- Umberto Eco
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100224/21702d0c/attachment.html>


More information about the NumPy-Discussion mailing list