0

This question is purely out of curiosity and I am sure the answer is compiler dependent. This also is an extreme micro optimization that will almost certainly lead to unmaintainable code. The CPU obviously needs to perform each calculation and store it someplace so maybe that is functionally the same.

With all that aside I was wondering if using less variables would inherently lead to faster executing code. For instance it would know to leave the results in the registers for long calculations. Of course if you need the same result more than once this is not helpful.

For instance this code:

var result = GetNumber();
var adjusted1 = result + 10;
var adjusted2 = adjusted1/2;
var final = GetFinal(adjusted2);

can be converted into the following:

var final = GetFinal((GetNumber() + 10)/2);

In general would the compiler output of something like this, single line with no variables, always be identical across languages? If the statement above involved 100's of calculations would it be the same?

Telavian
  • 3,752
  • 6
  • 36
  • 60
  • You could [check the assembly code](http://stackoverflow.com/a/137074/2721883) emitted for both. – clcto Jan 31 '17 at 20:06
  • That is true. I am wondering more in the general case however. – Telavian Jan 31 '17 at 20:17
  • 1
    There is no answer. For example in languages with pure functions and lazy eval, the one-liner argument of `GetFinal` may not even be evaluated if not used, while the four line version would. This even before considering compilation (or languages where + doesn't mean arithmetic plus). With compilation then, every compiler is free to have its implementation as long as it is conforming and such optimisations are not enforced. In general, do macro-optimisation and let the micro-optimisation to the compiler, focus on readability when in doubt. – Margaret Bloom Feb 01 '17 at 12:21

2 Answers2

2

The question is really compiler-specific, not language-specific. And since any given language might have any number of compilers, some of them not yet written, it is impossible to provide a general answer.

Most compilers will automatically remove unnecessary local temporary variables, if possible. Obviously the value needs to be stored somewhere in passing, but there may be no need to store it in main memory unless it is referred to later in the program, and even then it might still be available in a register. This optimization is relatively simple so I'd expect it to be done by any compiler which does any optimization.

Even if a compiler doesn't optimize here, the hardware may help by doing the store in parallel. Even if the compiler writes and immediately reads the value from main memory, the processor is probably going to grab it from the cache, which will be only slightly slower than a register access.

There are languages in which this optimization would be forbidden because variables are sacred, or, to put it more secularly, because the semantics of the language allows introspection or runtime interpretation (for example, using an eval builtin function). In that case, the variable must exist even though it is not visibly referenced, because some use of introspection or dynamic evaluation might refer to it. That also means that creating variables is a non-trivial operation, since it means storing them with their names in some kind of persistent datastructure. However: languages of this type are rarely compiled, so the entire program is probably suffering from interpretation overheads much more significant than the cost of an extra variable.

On the whole, you should write programs in a way which makes them most easy to understand. That will help you avoid bugs, help other people understand your code, and even help a compiler find the best optimization. It is hardly ever a useful expenditure of cerebral effort to worry about trivial details of optimization. Instead, concentrate on finding the best algorithm for each task.

rici
  • 234,347
  • 28
  • 237
  • 341
1

Depending on language and compiler as you mentioned, the compiler will at times optimize the code for you. In todays world, you really won't notice the difference between your example. More importantly, would be maintainability and readability of your code. You would not get much benefit from doing the latter unless you were doing say some algorithm development and hundreds of thousands of iterations or on an embedded processor where every millisecond counted. Branch predications and cache misses might be a different case.

arias_JC
  • 549
  • 3
  • 15