Optimizing a program is not a magical process. It is done by following a simple algorithm, synthesized by Stefan Schwarzer at Europython 2006 in his original pseudocode example:
def optimize(): """Recommended optimization""" assert got_architecture_right(), "fix architecture" assert made_code_work(bugs=None), "fix bugs" while code_is_too_slow(): wbn = find_worst_bottleneck(just_guess=False, profile=True) is_faster = try_to_optimize(wbn, run_unit_tests=True, new_bugs=None) if not is_faster: undo_last_code_change() # By Stefan Schwarzer, Europython 2006
This example probably isn't the neatest and clearest one but captures pretty much all the important aspects of an organized optimization procedure. The main things we learn from it are:
Optimization is an iterative process where not every...