I recently read that in python, using divide rather than multiplying by a decimal is not that much slower. There are certain code systems where it is, and others where it isn’t. A language like python already does a lot of interpretation, so such optimizations don’t make much impact.
I’m sure planning is important, but it should take the form of testing. Write two functions and then stress test them. If there’s not a big difference in performance, choose the least complex one or the one with least dependencies. If there’s a huge difference you could choose the faster one.
If you want to stress test a function you can try running the function several hundred times in a single logic tic:
for _ in range(500):
do_function()
The resultant impact on performance won’t be realistic but it is good for drawing comparisons between two different functions, as the margin of difference will be hugely magnified, enough so you can actually see it in a difference in frame rate.
Of course there’s a difference between optimization and just writing better code. If your code is not well written it will run slowly. For example, if you have to iterate through every object make sure you only do it once.
When you make a scene manager but only have one function which uses the data, what you’re really doing is this:
enemy_list = []
for ob in scene.objects:
if ob.get("enemy"):
enemy_list.append(ob)
for enemy in enemy_list:
do_something(enemy)
When instead you could do this:
for ob in scene.objects:
if ob.get("enemy"):
do_something(ob)
Which is actually much faster.
But if you want to sort the list (by distance from the player for example) creating a master list can be useful, but only if you can be sure it won’t change during the frame.
Another thing to remember is to remove, or comment out, print() statements when checking the speed of a function. You might be depressed that a function is running slowly and ruining your game, but it’s possible that removing print() will solve all problems.