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@ando ~]$ python -m timeit -s "from array import array" "array('b',
xrange(100))"
100000 loops, best of 3: 19.1 usec per loop
[steve@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@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@ando ~]$ python -m timeit -s "arr = range(100)" "sum(arr)"
100000 loops, best of 3: 2.34 usec per loop
[steve@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
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor