I am not showing any of my optimisation run in this series, which means that you unfortunately miss a lot of staring at and running of code while I ask myself questions like “What am I doing here?” and “Why did I do that?”.
Often my basic solution can be modified to meet at least one of the goals, though sometimes one must rethink your entire approach. Sometime the changes needed for code that meets one goal to meet the other are surprisingly subtle. See the code I have provided for floor 32 for an example.
Here are a couple of tips when going for speed:
- Iteration is really useful. Start with a working solution, then tweak it.
- It can be worthwhile to check for edge cases which are trivially solved.
- Try moving your output steps to the beginning and use a jump statement to skip them the first time. It is the same number of instructions, but fewer of them are used each time.
- Remember to use what you are holding after a check. Jump Zero means you are holding a 0, you can use that to reset counters and pointers.
- Jump statements can take you in the middle of a loop, not just at the start. Consider where you want to enter a section of code the first time around
- While size and speed are generally correlated, often you can speed things up using specific, if slightly redundant code.
- You may be able to have a counter or pointer value do double duty, especially towards the end of the program.
What tips for speeding up your apps have you found?
Check Page 2 for the optimised code