Performance – meaning and key metrics
There are two key characteristics of code:
- Code quality: It has to be solid and flexible and have a good architecture
- Code performance: It has to be fast
Making the code architecture very solid and stable is the most important task, but we shouldn't forget about making it fast as well. Achieving high performance can be a tricky and dangerous task. Here are a few things that you should keep in mind while working on performance improvement:
- Don't optimize your code upfront
There are many articles about this topic, why it's dangerous, and why you shouldn't do it. Just don't do it, and as Donald Knut says:
"Premature optimization is the root of all evil"
- Measure first
Firstly, don't optimize upfront, and secondly, measure first. Measure the code's performance characteristics and optimize only those parts that are slow. Almost 95 percent of code doesn't require performance optimization.
I totally agree with these points, but there is another type of performance optimization that we should think of upfront.
Everyday code performance
The small decisions that we make every day include the following:
- What type should it be,
Int
orString
? - Should I create a new class for a new functionality or add to an existing one?
- Use an array? Or maybe a set?
It seems as if these don't have any impact on the application's performance, and in most cases, they don't. However, making the right decision not only improves an application's speed, but also makes it more stable. This gives higher performance in application development. The small changes that we make every day make a big impact at the end of the year.
The importance of performance
High performance is very crucial. The performance of an app is directly related to user experience. Users want to get results immediately; they don't want to wait for the view to load, see a long Loading indicator, or see a lagging animation.
Every year, our computers and devices become more and more powerful, with more CPU speed, memory, storage, and storage speed. Performance problems could seem irrelevant because of this, but the software complexity increases as well. We have more complex data to store and process. We need to show animations and do a lot of other things.
The first way of solving a performance problem is by adding more power. We can add more servers to handle data, but we can't update our clients' PC and mobile devices. Also, adding more power doesn't solve the code performance issue itself, but just delays it for some time.
The second, and correct, solution is to remove the issue that causes the performance problem. For that, we need to identify the problem, the slow piece of the code, and improve it.
The key metrics
There are many things that impact an application's performance and user experience. We will cover the following key metrics:
- Operations' performance speed
- Memory usage
- Disk space usage
The most important of these and the one that has the biggest impact is the operations' performance speed. It tells us how fast a particular task can be performed, for example, creating a new user, reading from a file, downloading an image, searching for a person with a particular name, and so on.