2
I made two ways to change the value a
and b
, but which is more optimized, taking this example to other languages like desktop or web.
No use auxiliary variable:
$a = 10;
$b = 5;
$a = $b+$a;
$b = $a-$b;
$a -= $b;
Using auxiliary variable:
$a = 10;
$b = 5;
$c = $a;
$a = $b;
$b = $c;
Passing this same code to Java or C#, etc, would it be possible to measure precisely the difference between them ? For example, the space used in memory, or processing used.
– Lucas Caresia
There’s always a measure.
– Maniero
But it can complement your answer with how to measure ??
– Lucas Caresia
There are many forms, you are still adding other languages, each one has its own way. When you ask a question you have to decide what you want to know first. The general way is to have it done and take the execution time. I have an answer on this: https://answall.com/q/89622/101
– Maniero
Perhaps, the memory allocation request compensates for the time of arithmetic operations, usually asking the OS for resources is quite slow, don’t you think?
– Intruso
You’re not gonna ask the operating system for anything. If it is PHP, the memory is managed and does not keep asking for every necessary allocation and even if it asked to allocate accurate operating system, assign does not need. In this case, as it is in the answer. is allocating and assigning the same number of times in both cases, the only difference is that one does arithmetic operations and the other does not.
– Maniero