now I"m going to implement a life game in a serial way, which is on a 100000 x 100000 chessboard and spawns for 500 generations. I chose to use list to form a two-dimensional array to hold the data on the chessboard, but in the middle of the program, I mistakenly said memory error
. If the amount of data is small, there will be no problem at run time. How should I improve it?
-sharp
row = 100000
column = 100000
-sharp
count = 50
nCellSta = [[random.randint(0, 1) for i in range(column)] for j in range(row)]
nTempSta = [[0 for p in range(column)] for q in range(row)]
def CellCount(nRow, nColumn):
global row, column, nCellSta
nSum = 0
for i in range(nRow - 1, nRow + 2):
for j in range(nColumn - 1, nColumn + 2):
if i < 0 or i > row - 1 or j < 0 or j > column - 1 or i == nRow and j == nColumn:
continue
if nCellSta[i][j] == 1:
nSum += 1
if nSum == 0 or nSum == 1 or nSum == 4 or nSum == 5 or nSum == 6 or nSum == 7 or nSum == 8:
return 0
elif nSum == 2:
return nCellSta[nRow][nColumn]
elif nSum == 3:
return 1
-sharp nCellSta
def printValue():
nSum = 0
global row, column
for i in range(row):
for j in range(column):
-sharp
-sharp print(nCellSta[i][j], " ", end="")
nSum += nCellSta[i][j]
-sharp print("\n")
return nSum
def main():
global count, nCellSta, nTempSta
printValue()
startTime = time.perf_counter()
for k in range(count - 1):
-sharp print(":\n")
for i in range(row):
for j in range(column):
nTempSta[i][j] = CellCount(i, j)
nCellSta = copy.deepcopy(nTempSta)
if not printValue():
print("")
break
endTime = time.perf_counter()
seconds = endTime - startTime
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print(":%d:%02d:%.3f" % (h, m, s))