Reputation: 1119
I have an object class:
class CellDynamicState():
def __init__(self):
self.ids_here = []
self.name = "xyz"
self.cell_static_occupied = False
And I create a list of 10,000 of them:
dynamic_cell_grid = [[CellDynamicState() for y in range(100)] for x in range(100)]
Then later I need to copy this array:
copy.deepcopy(dynamic_cell_grid)
This is slow, half a second or more.
I'm looking for suggestions for a better approach to this. If the sub-fields were all numeric, or even of known length, I would consider using numpy arrays - and I may go in that direction (using a max length for ids_here, and a dictionary for any dynamic members I might wish to assign to a cell.) But are there any other interesting ideas?
EDIT
For interest, this is how I am looking at converting to using np arrays:
class DynamicState():
def __init__(self):
self.n_ids_here_array = np.zeros([x_side, y_side], dtype=np.int)
self.ids_here_array = np.zeros([x_side, y_side, consts.max_ids_at_cell], dtype=np.int)
self.static_occupied = np.zeros([x_side, y_side], dtype=np.bool)
def remove_id_here(self, ss, cx, cy, id):
found = False
for i in range(self.n_ids_here_array[cx,cy]):
if self.ids_here_array[cx,cy,i] == id:
self.ids_here_array[cx,cy,i] = self.ids_here_array[cx,cy,self.n_ids_here_array[cx,cy]-1]
found = True
assert found, "didn't find id"
self.n_ids_here_array[cx,cy] -= 1
def add_id_here(self, ss, cx, cy, id):
assert self.n_ids_here_array[cx,cy] < consts.max_ids_at_cell - 1, "too many ids at cell"
self.ids_here_array[cx,cy,self.n_ids_here_array[cx,cy]] = id
self.n_ids_here_array[cx,cy] += 1
Upvotes: 0
Views: 45
Reputation: 762
this code runs in 0,006.775900010325131 ms on my machine
import numpy as np
array = np.full((100, 100), CellDynamicState())
A nother option is this:
array = np.zeros(shape=(100, 100), dtype=CellDynamicState)
which runs in 0.004143754999859084 ms
Upvotes: 2