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?
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.