Skimming through the code, I can’t really see anywhere to improve other than you are running deepcopy()
over and over again.
However, I would recommend you to do profiling
. If you are using pycharm, you can do profiling using the clock/run
sign.
I am sure other IDEs also have such capabilities. This way you can figure out which function is taking the most time.
Whole graph:
When I zoom in to a few functions (I am showing google cloud functions):
You can see how many times they are called and how long they took etc.
Long story short, you need a profiler!
CLICK HERE to find out more related problems solutions.