I’m trying to speed up part of my code that involves looping through and setting the values in a large 2D array. One of the suggestions was that I try pre-allocating the array rather than using .append() but it was pointed out that in Python .append() is an amortized O(1) operation.
However when I tested it using the following code:
import time
x = list()
z = list()
t1 = time.time()
for i in range(10000):
z.append([])
for j in range(10000):
z[i].append(0)
t1 = time.time()
for i in range(10000):
x.append([])
for j in range(10000):
x[i].append(1)
print(time.time()-t1)
t1 = time.time()
for i in range(10000):
for j in range(10000):
z[i][j] = 1
print(time.time()-t1)
I consitently get the pre-allocated array taking 3-4 seconds less than the array that isn’t preallocated (~17s compared to ~21). What is it in this code that is causing the .append() based function to take longer than replacing the value in a pre-allocated array?
Consider the following:
Compared with:
How you approach things can make a huge difference.