A quick bit of background before my question. I came across this while refactoring a Tetris game.
Representing a Tetris piece can be done in the following ways (I will use the 'L' shape as an example):
an array of booleans:
boolShape = [False, False, False, False,
False, True, True, False,
False, False, True, False,
False, False, True, False]
an array of chars (I guess 'strings of length 1' since this is Python):
charShape = ['.', '.', '.', '.',
'.', 'X', 'X', '.',
'.', '.', 'X', '.',
'.', '.', 'X', '.']
an array of ints:
intShape = [0, 0, 0, 0,
0, 1, 1, 0,
0, 0, 1, 0,
0, 0, 1, 0]
a numpy array of ints:
npShape = np.array([0, 0, 0, 0,
0, 1, 1, 0,
0, 0, 1, 0,
0, 0, 1, 0])
a numpy array of booleans:
npBoolShape = np.array([0, 0, 0, 0,
0, 1, 1, 0,
0, 0, 1, 0,
0, 0, 1, 0], dtype=bool)
When I use sys.getsizeof(array) and sys.getsizeof(array[i]) to get the size of each array and of a single element, I get the following:
Bool | Array: 184 | Item: 24
Char | Array: 184 | Item: 50
Int | Array: 184 | Item: 24
NP | Array: 224 | Item: 32
NPBool| Array: 112 | Item: 25
Can anyone explain why an individual char is larger than an int but an array of 20 is the same size as 20 ints?
Does it make any difference in terms of performance to implement the game in one of the arrays described above? It makes no difference for a human player, but, to train an AI, many iterations are needed and time becomes an issue.