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

前端 未结 9 1980
执念已碎
执念已碎 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:28

    I suppose this is more of an answer than you're looking for, but here goes...

    SO is filled with questions where people are worried about the performance of X, Y, or Z, and that worry is a form of guessing.

    If you're worried about the performance of something, don't worry, find out.

    Here's what to do:

    1. Write the program

    2. Performance tune it

    3. Learn from the experience

    What this has taught me, and I've seen it over and over, is this:

    • Best practice says Don't optimize prematurely.

    • Best practice says Do use lots of data structure classes, with multiple layers of abstraction, and the best big-O algorithms, "information hiding", with event-driven and notification-style architecture.

    • Performance tuning reveals where the time is going, which is: Galloping generality, making mountains out of molehills, calling functions & properties with no realization of how long they take, and doing this over multiple layers using exponential time.

    • Then the question is asked: What is the reason behind the best practice for the big-O algorithms, the event- and notification-driven architecture, etc. The answer comes: Well, among other things, performance.

    So in a way, best practice is telling us: optimize prematurely. Get the point? It says "don't worry about performance", and it says "worry about performance", and it causes the very thing we're trying unsuccessfully not to worry about. And the more we worry about it, against our better judgement, the worse it gets.

    My constructive suggestion is this: Follow steps 1, 2, and 3 above. That will teach you how to use best practice in moderation, and that will give you the best all-around design.

提交回复
热议问题