- Hands-On High Performance Programming with Qt 5
- Marek Krajewski
- 145字
- 2021-07-02 13:53:58
Low-hanging fruit
As we already said in Chapter 1, Understanding Performant Programs, there are some techniques that are so simple that they cost you nothing and buy you small but consistent performance gains.
They are more like habits than real techniques, to be frank. In C++ such absolute basics are, for example, as follows:
- Using ++i instead of i++—a compiler should optimize that away, but why rely on that?
- Passing function arguments by const reference—note that, in C++11, we may choose to pass by a value in cases when we will copy it inside the function. For details, look at the discussion about move constructors later.
- Using initialization over assignment—for example, when initializing class members in a constructor, look at the discussion of temporaries later.
Be aware: not heeding this basic advice would amount to premature pessimization!