Reputation: 1345
I have a class in python3 that contains a few variables and represents a state. During the program (simulation) I need to make a big amount of copies of this state so that I can change it and still have the previous information.
The problem is that deepcopy
from the copy
module is too slow. Would I be better of creating a method in that class to copy an object, which would create and return a new object and copy the values for each variable? Note: inside the object there is a 3D list as well.
Is there any other solution to this? The deepcopy
is really too slow, it takes more than 99% of the execution time according to cProfile
.
Edit: Would representing the 3D list and other lists as numpy
arrays/matrix and copying them with numpy
inside a custom copy function be the better way?
Upvotes: 0
Views: 1373
Reputation: 1345
For people from the future having the same problem: What I did was creating a method inside the class that would manually copy the information. I did not override deepcopy, maybe that would be cleaner, maybe not.
I tried with and without numpy for 2D and 3D lists, but appending 2 numpy arrays later in the code was much more time consuming than making a sum of 2 lists (which I did need to do for my specific program).
So I used:
my_list = list(map(list, my_list)) # for 2D list
my_list = [list(map(list, x)) for x in my_list] # for 3D list
Upvotes: 1