Variables Unlike Class Calls

Recently, I learned that if you have a reference to a class as a parameter of a function, it is better to practice and more efficiently store certain necessary pieces of information in the form of local variables, rather than referring to members of classes every time you need them in a function.

So...

void function(const Sphere& s) { //lots of calls to s.centre and s.radius } 

or

 void function(const Sphere& s) { Vector3 centre = s.centre; float radius = s.radius; //an equal amount of calls to centre and radius } 

They say the second is better, but why? And also, where is a good place to start exploring it more fully? (for dummys please!)

+4
source share
6 answers

The one who told you this probably thought the second version was likely to be faster.

I think these are bad tips for two reasons:

  • It may or may not be faster. It depends on the compiler, what the code does, etc.
  • Even if it's faster, it screams premature optimization . Micro-optimization should only be optimized after profiling the code and determining which part of the code is the common bottleneck.
+9
source

The concept is intuitive, but incorrect. The concept is that accessing members takes more computation than local variables, so by converting members into variables you maintain performance.

But it's not right. Your compiler will optimize this the way you could never have imagined. Do not try to be smarter than the compiler.

+8
source

It may be dangerous. If s.centre or s.radius changes during the execution of this function (for example, due to the function you are calling or another thread), you will get two inconsistent old values ​​in your local variables - causing errors. Even if you do it safely, why risk the occurrence of errors when you can just go back to the canonical variables yourself?

+6
source

You lied. Moreover, you should not worry about such trifles if you did not profile your code and did not find this the main source of inefficiency of your code. Do not try to outsmart the compiler optimizer. This is better for optimizing your code than you.

+5
source

Here is a general forecast for your code:

 void function(Sphere s) { Vector3 centre = s.centre; float radius = s.radius; //an equal amount of calls to centre and radius } 

First, you get much more efficiency by passing Sphere as a const reference. Thus, a new copy is not created, which is probably more expensive than access to membership. So the way:

 void function(const Sphere& s) { Vector3 centre = s.centre; float radius = s.radius; //an equal amount of calls to centre and radius } 

Secondly, you should not directly address class members. It may be easy now, but in a large project it is very difficult to debug. You must use the built-in receivers and setters. Thus, the generated code is the same, but you have one entry point.

Thirdly, this version is not thread safe. What if another thread modifies s ? s.center and s.radius would change, but you would still work with the old values.

Finally, compilers do better optimization than you can, so it's best to leave this to the compiler.

+2
source

The one who told you this is wrong. You can increase productivity using the built-in functions. Also in your example use

 void function(Sphere &s) 

Saves the use of the copy constructor.

0
source

All Articles