[Tutor] A CSV field is a list of integers - how to read it as such?
Steven D'Aprano
steve at pearwood.info
Mon Mar 4 15:01:22 CET 2013
On 05/03/13 00:24, Dave Angel wrote:
> import array
> myarray = array.array('b', mylist)
>
> An array is somewhat slower than a list,
I think that it's true that using a list *can* be faster, but that's only because we're comparing apples with oranges. Arrays do more work than lists. For example, using Python 2.7 constructing an array is much slower than constructing a list:
[steve at ando ~]$ python -m timeit -s "from array import array" "array('b', xrange(100))"
100000 loops, best of 3: 19.1 usec per loop
[steve at ando ~]$ python -m timeit "list(xrange(100))"
100000 loops, best of 3: 3.26 usec per loop
but that's only because the list code doesn't perform the same range checking as the array does. If we add range checking ourselves, we see very different results:
[steve at ando ~]$ python -m timeit "list(x for x in xrange(100) if 0 <= x < 256)"
10000 loops, best of 3: 27.4 usec per loop
So I would suggest that constructing an array is significantly faster than constructing a restricted list.
Here's another example: summing a list versus an array. In this specific example, there is a small but consistent advantage to lists, but probably not one that's worth caring about:
[steve at ando ~]$ python -m timeit -s "arr = range(100)" "sum(arr)"
100000 loops, best of 3: 2.34 usec per loop
[steve at ando ~]$ python -m timeit -s "from array import array" -s "arr = array('b', range(100))" "sum(arr)"
100000 loops, best of 3: 2.78 usec per loop
--
Steven
More information about the Tutor
mailing list