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 would have 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 the 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 have 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...