Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any performance penalties when using nested structures?

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

Thanks

like image 207
Faken Avatar asked Sep 16 '10 23:09

Faken


1 Answers

Not really. Classes/structs are just defining offsets into memory, so if you have a class within a class within a class, the compiler just adds up the offsets.

Performance comes into play once you have pointers (each pointer dereference is a memory read and potential L2 cache miss) or virtual functions (very bad, especially on older CPUs).

EDIT: One thing I should note though - if you're developing an application where performance is not absolutely crucial, focus on good class design rather than performance. While things like L2 cache misses make a big difference when you're writing something that needs to run at 60fps, it is of little relevance in a normal desktop application.

like image 80
EboMike Avatar answered Oct 20 '22 12:10

EboMike