Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy vs list comprehension, which is faster? [closed]

Tags:

python

numpy

I ran a simple speed test comparing numpy and python list comprehension, and apparently list comprehension was faster. Is that correct?

import sys, numpy
from datetime import datetime

def numpysum(n):
    a = numpy.arange(n) ** 2
    b = numpy.arange(n) ** 3
    return a + b

def pythonsum(n):
    a = [i ** 2 for i in range(n)]
    b = [i ** 3 for i in range(n)]
    return [a[i] + b[i] for i in range(n)]

size = 10
start = datetime.now()
c1 = pythonsum(size)
delta1 = datetime.now() - start

start = datetime.now()
c2 = numpysum(size)
delta2 = datetime.now() - start

print c1
print c2

print delta1
print delta2
like image 319
vgoklani Avatar asked Mar 14 '12 19:03

vgoklani


People also ask

Is list comprehension faster than NumPy?

Numpy is more the 61 times faster than list comprehension, but sometimes we need to use this value as a list. Even as a list, the method Numpy is at least 2,34 faster than other methods.

What is faster than list comprehension?

For loops are faster than list comprehensions to run functions.

How much faster is NumPy than lists?

Because the Numpy array is densely packed in memory due to its homogeneous type, it also frees the memory faster. So overall a task executed in Numpy is around 5 to 100 times faster than the standard python list, which is a significant leap in terms of speed.

Are NumPy operations faster?

NumPy is fast because it can do all its calculations without calling back into Python. Since this function involves looping in Python, we lose all the performance benefits of using NumPy. For a 10,000,000-entry NumPy array, this functions takes 2.5 seconds to run on my computer.


2 Answers

I think you might want to consider varying your testing parameter:

In [39]: %timeit pythonsum(10)
100000 loops, best of 3: 8.41 us per loop

In [40]: %timeit pythonsum(100)
10000 loops, best of 3: 51.9 us per loop

In [41]: %timeit pythonsum(1000)
1000 loops, best of 3: 451 us per loop

In [42]: %timeit pythonsum(10000)
100 loops, best of 3: 17.9 ms per loop

In [43]: %timeit numpysum(10)
100000 loops, best of 3: 13.4 us per loop

In [44]: %timeit numpysum(100)
100000 loops, best of 3: 17 us per loop

In [45]: %timeit numpysum(1000)
10000 loops, best of 3: 50.3 us per loop

In [46]: %timeit numpysum(10000)
1000 loops, best of 3: 385 us per loop

Ratio of Numpy vs List comprehension timings:

10: 0.6x

100: 3.1x

1000: 9x

10000: 46x

Thus, Numpy is much faster for large N.

like image 84
JoshAdel Avatar answered Nov 15 '22 06:11

JoshAdel


Your size is too small. I tried again with size=1000000 and numpy outperformed the list comprehension by 9x.

I'm guessing numpy has a higher setup overhead, but in general for non-trivial input sizes (10 is definitely trivial) you can expect it to be at least as fast as a list comprehension, and in most cases much faster.

like image 35
Eduardo Ivanec Avatar answered Nov 15 '22 07:11

Eduardo Ivanec