Questions with similar titles are about Python lists or NumPy. This is about the array.array class part of the standard Python library, see https://docs.python.org/2/library/array.html
The fasted approach I came up with (for integer types) is to use array.fromfile with /dev/zero. This is
- about 27 times faster than array.array('L', [0] * size), which temporarily requires more than twice the memory than for the final array,
- about 4.7 times faster than arrar.array('L', [0]) * size
- and over 200 times faster than using a custom iterable object (to avoid creating a large temporary list).
However, /dev/zero may be unavailable on some platforms. Is there a better way to do this without NumPy, non-standard modules or my own c-extension?
Demonstrator code:
import array
import sys
import time
size = 100 * 1000**2
test = sys.argv[1]
class ZeroIterable:
def __init__(self, size):
self.size = size
self.next_index = 0
def next(self):
if self.next_index == self.size:
raise StopIteration
self.next_index = self.next_index + 1
return 0
def __iter__(self):
return self
t = time.time()
if test == 'Z':
myarray = array.array('L')
f = open('/dev/zero', 'rb')
myarray.fromfile(f, size)
f.close()
elif test == 'L':
myarray = array.array('L', [0] * size)
elif test == 'S':
myarray = array.array('L', [0]) * size
elif test == 'I':
myarray = array.array('L', ZeroIterable(size))
print time.time() - t