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

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

    These 2 bits of code are identical:

    Point x;
    int l=x.getLength();
    
    int l=GetLength(x);
    

    given that the class Point has a non-virtual method getLength(). The first invocation actually calls int getLength(Point &this), an identical signature as the one we wrote in our second example. (*)

    This of course wouldn't apply if the methods you're calling are virtual, since everything would go through an extra level of indirection (something akin to the C-style int l=x->lpvtbl->getLength(x)), not to mention that instead of 2 int's for every pixel you'd actually have 3, the extra one being that pointer to the virtual table.

    (*) this isn't exactly true, the "this" pointer is passed through one of the cpu registers instead of through the stack, but the mechanism could have easily worked either way.

提交回复
热议问题