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
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
To initialize an array with the default value, we can use for loop and range() function in python language. Python range() function takes a number as an argument and returns a sequence of number starts from 0 and ends by a specific number, incremented by 1 every time.
The zeros() function is used to get a new array of given shape and type, filled with zeros. Shape of the new array, e.g., (2, 3) or 2. The desired data-type for the array, e.g., numpy. int8.
To initialize an array in Python, use the numpy. empty() function. The numpy. empty() function creates an array of a specified size with a default value = “None“.
Updated to Python 3 and added the 'B' method:
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))
elif test == 'B':
myarray = array.array('L', bytes(size * 8))
print(len(myarray))
print(time.time() - t)
The 'S' method (array.array('L', [0]) * size
) wins:
$ python3 --version
Python 3.7.3
$ python3 z.py Z
100000000
1.1691830158233643
$ python3 z.py L
100000000
2.712920665740967
$ python3 z.py S
100000000
0.6910817623138428
$ python3 z.py B
100000000
0.9187061786651611
$ python3 z.py I
100000000
62.862160444259644
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With