Timing NumPy

I've been hearing a lot of good things about NumPy and Python- so much so that I've finally had to give it a try. What's so good about NumPy? For one thing, its built-in array data structure. Given that data arrays are one continous block of memory, referencing numbers directly in arrays would be much faster than referencing them in Python's built-in lists- which would really be a list of pointers to the real location of data.

from timeit import timeit

test_list_loop = '''
    alist = [0] * 1000
    for i in xrange(0, len(alist)): 
        alist[i] += 1
'''

test_numpy_arrays = '''
    a = np.zeros(1000)
    b = a+1
'''

print( timeit( stmt=test_numpy_arrays, setup='import numpy as np',  number=100000 ) )
print( timeit( stmt=test_list_loop, number=100000 ) )

The result?

0.680721998215
7.49408698082

It's fairly conclusive.

NumPy arrays are 11 times faster than Python lists. No contest.