C++: Performance impact of BIG classes (with a lot of code)

前端 未结 9 1987
执念已碎
执念已碎 2021-01-17 15:08

I wonder if and how writing \"almighty\" classes in c++ actually impacts performance.

If I have for example, a class Point, with only uint x

9条回答
  •  无人及你
    2021-01-17 15:34

    First: do not optimize prematurely. Second: clean code is easier to maintain than optimized code.

    Methods for classes have the hidden this pointer, but you should not worry about it. Most of the time the compiler tries to pass it via register.

    Inheritance and virtual function introduce indirections in the appropriate calls (inheritance = constructor / destructor call, virual function - every function call of this function).

    Short:

    • Objects you don't create/destroy often can have virtual methods, inheritance and so on as long as it benefits the design.
    • Objects you create/destroy often should be small (few data members) and should not have many virtual methods (best would be none at all - performance wise).
    • try to inline small methods/constructor. This will reduce the overhead.
    • Go for a clean design and refactor if you don't reach the desired performance.

    There is a different discussion about classes having large or small interfaces (for example in one of Scott Meyers (More) Effective C++ Books - he opts for minimal interface). But this has nothing to do with performance.

提交回复
热议问题