In terms of readability and performance, should I pre-allocate memory for an array using [None]*n? Is allocating an empty one [] and using .append() over and over considered wasteful?
In terms of readability and performance, should I pre-allocate memory for an array using [None]*n? Is allocating an empty one [] and using .append() over and over considered wasteful?
In this simple timing test, the use of [None] * n does indeed appear to be slightly quicker, but arguably not by enough to justify adopting this approach over the more usual idioms.
import time
def func1(size):
a = [None] * size
for i in range(size):
a[i] = i
def func2(size):
a = []
for i in range(size):
a.append(i)
def func3(size):
a = [i for i in range(size)]
size = 1000000
repeat = 100
t0 = time.time()
for _ in range(repeat):
func1(size)
t1 = time.time()
for _ in range(repeat):
func2(size)
t2 = time.time()
for _ in range(repeat):
func2(size)
t3 = time.time()
print(t1 - t0, t2 - t1, t3 - t2)
Results:
[None * size] and then index: 4.82 secondsappend in a loop: 6.37 secondsRepeating the tests with size=1000 and repeat=100000 give similar results:
[None * size] and then index: 3.16 secondsappend in a loop: 4.88 secondsAnd again with size=10 and repeat = 10000000:
[None * size] and then index: 6.09 secondsappend in a loop: 7.65 seconds