## Fixing the algorithm

My preferred approach to improving performance is—always—fixing the algorithm. Look at it this way—if we need *1* time unit to process one data item, and the algorithm is *O(n ^{2})*, we need

*10,000*time units to process an input of size

*100*. If you fine-tune the code and speed up the operation by 50% (which is an excellent result), the code will need

*5000*time units to do the job. If you, however, change the algorithm to

*O(n log n)*, it will need in the order of

*1000*time units or less. Even if processing one item takes 100% longer than in the original code, the whole process will run in, say,

*2000*time units.

### Note

An algorithm with lower complexity will beat an algorithm with higher complexity, even if the latter executes its steps faster.

As it's impossible to give one piece of advice that will fix all your problems, Chapter 2, *Fixing the Algorithm*, looked into different user stories. The first topic was about responsive user interfaces. A program's UI can lag or block for different...