Almost, I fixed it for you. Your method had some redundancies that slowed it down.
var matrix = []
for x in range(width):
matrix.append([])
for y in range(height):
matrix[x].append(0)
matrix[x]=[]
this sets the [] you just appended into a []
matrix[x].append([])
matrix[x][y]=0
these append a [] and then set the [] to a 0, which is weird and inefficient
the cleaned up method simply starts with a [], then adds [] as rows, ex: [ [],[],[] ]
then adds 0's into the rows, ex: [ [0,0,0],[0,0,0],[0,0,0] ]
one last side note: if you are making a very large 2D array (300x300 or higher), it seems to be faster to use a while loop instead of the for each loop with the range
method, because the range
method does something along the lines of building an array, ex: range(3) makes an array = [0,1,2]. This is slower than simply having an increment counter, ex: int x = 0, while(x<3), x += 1