I'm classically trained psychologist, not a programmer, so sometimes the more advanced aspects of programming escape me, in particular regarding program efficiency and/or certain best practices, in this case with regard to the use of variables.

If so, is this gain or loss in efficiency generally the same across languages, or does it vary by language? Are there thresholds where it becomes better to name a variable as opposed to using a repeated function call or vice versa? What aspects might change its efficiency (e.g., is there a difference whether it's a member function of a class vs. a regular function in the global scope)? etc.

If possible, I'd like to know specifically how such a notion applies to C++/MFC dialogs, as it arose when I was writing some code in that framework.

5 Answers
5

To answer your question: It is generally more efficient to store data in a variable and reference that. Additional local variables are cheap.

Starting with a simple example, suppose you have the code fragment:

x = 5;
y = x*x + 1;
z = x*x + 2;

If you look at the above code and pretend you're the CPU executing it step by step, the CPU would do the multiplication twice with the same value of x. That's almost always less efficient than doing the multiplication once:

x = 5;
x2 = x*x;
y = x2 + 1;
z = x2 + 2;

Now, modern compilers almost always have an optimisation called common subexpression elimination, which will have the same effect of extracting x2 as in the above example. So you often don't have to worry about it, because the compiler has your back.

Having said that, using a variable such as x2 might substantially reduce the complexity of the following lines, so there's nothing wrong with introducing a variable like that for readability reasons.

In the case of your MFC code, you are repeatedly calling pLISTBOX->GetItemData(SelectedIndex). Since this is a function call that makes a call into the OS to get further data, the compiler can't do common subexpression elimination on that. Instead, I would introduce a new variable so you only have to do the call once:

Might want to add that CSE only works in the original example if the called function (GetSomeInformation) is pure and the compiler is aware of this fact. Otherwise, the compiler has to call it three times to make sure the side effects happen as expected.
–
tdammersFeb 6 '13 at 22:58

1

+1 for citing the optimizer. The OP needs to understand that source code is merely advice to the compiler on how to create object code.
–
Ross PattersonFeb 7 '13 at 1:16

In most cases, >90% of your program's time will be spent in <10% of the code, and without profiling your code, you have no idea which 10% that is. So don't even worry about it until you get feedback; it's much more valuable to optimize for programmer time than run time.

It is usually more efficient to store a computed value in a local variable, assuming your compiler didn't already optimize that for you. In fact, sometimes this is built into a function itself, in a technique called memoization.

However, most of the time, the efficiency gain is small enough to be considered negligible. Readability should usually be your primary concern after correctness.

That being said, using a local variable also often happens to make it more readable, giving you the best of both worlds. Using your example:

A general principle I follow is that if I don't know how a function is implemented, it might be expensive and if I know that I only need the value from that function once, I just store it locally. Your code example would then become: