Python implements life games with a large amount of data.

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))
Nov.09,2021

have you calculated the memory requirements? One element in
list occupies 4 bytes, so
1000000 arrays 2 * 4 / 1024 bytes 3 = 37G
the two arrays are 74G!
take into account other consumption, at least 80 gigabytes of memory.
ways to consider:
use bitarray bitarray , so that each element occupies 1 bit, and the memory is probably reduced to 1.5G
to improve the algorithm, and remove the temporary array Temp


is it because the intermediate result takes up too much memory, and the previous generations can be recycled


.
In [1]: import sys

In [2]: a = [0] * 100000

In [3]: len(a)
Out[3]: 100000

In [4]: sys.getsizeof(a)
Out[4]: 400036

In [5]: b = "0"*100000

In [6]: len(b)
Out[6]: 100000

In [7]: sys.getsizeof(b)
Out[7]: 100021

can be regarded as providing an idea

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bcd568-30b58.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bcd568-30b58.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?