Truths about code optimization
- Make sure you start with working code in the first place and have good unit tests so that speeding it up does not break anything.
- Don’t assume you know the problem. Run your code with a profiler and see which bits are the slow ones before you start to write new code. Your obvious best guess could well be wrong.
- As you optimize your code, run the profiler after every change to check whether the change actually helped and whether there is a new bottleneck now.
- It is obvious but if a change did not measurably help, take it out no matter how brilliant you think it was.
- You can always keep on making things faster, lighter, cheaper but it will also gradually make the code harder to read, longer, complex. Know when to stop.
Full post here, 5 mins read