Fine-tuning SlowCode
We are near the end of the chapter. Let’s step back a little and take a breath.
This chapter is packed with information, but if I had to pick the most important lesson, it would be: don’t enlarge strings (arrays, lists) element by element. If you know how much space some data will take, allocate space at the beginning.
As all recommendations go, this too must be taken with a grain of salt. Most of the time, such optimizations will not matter. You should always have the following checklist in mind:
- Measure. Find where the problem actually is.
- See if you can improve the algorithm. This will give the best results.
- Fine-tune the code.
In the previous chapter, we optimized the heart out of our old friend SlowCode
. Just to remind you, the latest version, SlowCode_Sieve_v2_opt
, processes 10 million elements in a mere 529 ms. Still, there is a little room for improvement that I’ve neglected so far.
The problem lies in...