Are there any performance penalties when using nested structures?

Are there any performance penalties when I use several nested structures / classes (sort of like using mutation heap arrays) or is it just an organizational feature of the language to make it easier to track data and the compiler doesn't really see any difference?

thank

+3
source share
4 answers

Not really. Classes / structures simply define offsets in memory, so if you have a class inside a class inside a class, the compiler just adds offsets.

, ( - L2) ( , ).

EDIT: , , - , , , . , L2, , -, 60 , .

+3

. , .

+1

Mostly not, as others have mentioned. However, there is a slight exception: placing structures inside structures can lead to a small restriction on memory usage due to alignment problems, compared to the same primitives that fit directly into one structure. This can lead to a failure in theoretical costs, which degrades performance. For instance:

#include <iostream>
using namespace std;  // So sue me

struct A {
    double d;
    int i;
};

struct B {
    int j;
    int k;
    int l;
};

struct AB {
    A a;
    B b;
};

struct C {
    double d;
    int i;
    int j;
    int k;
    int l;
};

int main() {
    cout << sizeof(AB) << endl;  // 32
    cout << sizeof(C) << endl;   // 24
}
+1
source

Short answer: None.

0
source

Source: https://habr.com/ru/post/1765209/


All Articles