Is it worthwhile to sacrifice code simplicity to improve performance ?, Cost-effective performance sacrifice code
The author of this article, Arne Mertz, is a C ++ enthusiast and has rich development experience. In this article, Arne Mertz elaborates on the relationship between conciseness and performance. He believes that developers should never sacrifice conciseness for performance, but should learn to use tools to solve performance problems.
The translation is as follows:
One of the strengths of C ++ is its ability to write very high-performance code. In practice, how can we grasp the performance processing scale?
Performance = Efficiency
First, we must clearly distinguish performance from efficiency. What do the two represent?
How fast we can do (performance );
How long does it take (efficiency) to complete ).
It seems similar, but it is not. For example, if you need to change from A to B, efficiency means "Shortest Path", and performance means "running on behalf ". Therefore, although high performance is achieved at Bolt speed, it is not efficient-"No Shortest Path is selected ".
For programs, loop usually takes a lot of time. In this case, the performance means "the shorter a single cycle is used, the better", and the efficiency means "Reducing the number of cycles as much as possible ".
Performance is not all about the program
This is a simple truth, but it is often overlooked, especially for new programmers. In many programming forums, questions about code performance optimization are everywhere.
There is a saying that 80% of the running time is determined by about 20% of the code, and 90%/10%. Therefore, for a program, the key operation code may only exist in a small part of the code. Therefore, if you focus on optimization of all the code without focusing on the key code, it is actually half the effort.
We really don't know how to write high-performance code?
In fact, the main factor determining the program running duration is the number of commands, but this is not controlled by us but controlled by the compiler and Its optimizer.
There are many optimizer types. Unless it is an expert in this field, it is difficult to understand which optimizations it has made to the Code. The optimizer can destroy temporary objects and inline functions to clear more commands.
So when these uncertainties exist, can we still write absolutely high-performance code? If you are really concerned about performance, I suggest using tools to help.
But don't be too pessimistic. If there are two or more methods to write the same readable code, you may wish to choose the highest-performance writing method. For example, you can use ++ iter instead of iter ++ without storing the results.
Performance is not always in conflict with simplicity
Another important factor affecting the running time is the data layout and structure in the memory.
In addition, if the data memory layout is not good, it will take a lot of time to get data and cause command redundancy.
Summary
We recommend that you compile readable and simple code by default. If you find that there are performance problems and you have found their location, there are still a lot of options to deal with them without having to write complicated code for the sake of speed. Never sacrifice simplicity for performance, and learn to use tools to solve performance problems.
So the question is, will you sacrifice code conciseness for performance?
Recommended reading:
Seven basic practical algorithms that programmers must know and their explanations
Top 10 programming taboos that programmers must pay attention
Parse several growth stages of programmers
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.