I'm learning python recently, and is doing many practice with the language.
One thing I found interesting is that, when I read from an array, it's almost half of the time slower than list. Does somebody know why?
here's my code:
from timeit import Timer
import array
t = 10000
l = range(t)
a = array.array('i', l)
def LIST():
for i in xrange(t):
l[i]
def ARRAY():
for i in xrange(t):
a[i]
print Timer(LIST).timeit(1000);
print Timer(ARRAY).timeit(1000);
the output is:
0.813191890717 1.16269612312
which indicates that reading array is slower than list. I think array is a fixed size memory, while list is a dynamic structure. So I assumed that array would be faster than list.
Does anyone has any explanation?