If givin some situation that you can do a loop of a certain event or function that needed to be solved using loops, where you can achieve these by any kind of loop. How can we determine the difference between each loops that can be used based on their speed, efficiency, and memory usage? Like for example, you have these loops
for(int i=0;i<10;i++) {
array[i] = i+1;
}
int i = 0;
while(i<10) {
array[i] = i+1;
i++;
}
The above example have the same output, of course you cannot see the difference between them when executed since this is just a small process, what if you are processing an enormous loop that’s eating up your memory? Which loop is better to use? Is there a proper measure when to use what loop?
Edit:
for pakore answer,
From your answer I can safely say that If I reorder my variables where most of the variables that are dependent are far from each other (with other lines inbetween) could have been more efficient. Say for example,
a=0;
b=1;
c=5;
d=1;
for(int i=0; i<10;i++)
{
a=b*CalculateAge()+d;
m=c+GetAvarage(a);
d++;
}
and
a=0;
b=1;
c=5;
d=1;
for(int i=0; i<10;i++)
{
a=b*CalculateAge()+d;
d++;
m=c+GetAvarage(a);
}
The later is more efficient than the first one since in the later I have called an outside method on the first line and the second line is independent from the result of the first line than on the third line.
Since the first example will wait for the result of the first line before executing the second line and on the third, and the second example has already executed the second line while waiting for the result of the first line.
Conclusion:
An optimized loop doesn’t mater what kind of loop you are using. As what pakore explained, and polygenelubricants, the main thing that you can mind in your loop is how your code is written inside. It is the compilers job to optimize your code, it also helps if you optimize your code according to its dependency with each variable as what pakore explain below.
Well, it’s difficult to explain all the logic behind a loop here.
The compiler will do amazing things for you in order to optimize the loops, so it does not matter if you use
whileorforbecause the compiler will translate to assembler anyway.In order to have a deeper understanding you should learn some assembler and then how a basic processor works, how it reads the instructions and how it processes them.
In order to improve pipelining, it’s better to place statements with the same variables far away from each other. This way, while one statement is calculated, the processor can take the next statement if it’s independent from the first one and start calculating it.
For example:
We have a dependency here between
aandband the statements are right next to each other. But we see thatmandiare independent from the rest, so we can do:So while
ais being calculated, we can start calculatingmandi. Most of the times the compiler detects this and does it automatically (it’s calle code reordering). Some times for small loops the compiler copy and pastes the code inside the loop as many times as it’s needed, because it’s faster not to have control variables.My suggestion is that let the compiler take care about these things and focus on the costs of the algorithms you are using, it’s better to reduce from O(n!) to O(logn) than to do micro-optimizations inside the loops.
Update according to the question modified
Well, the dependencies have to be write/write or read/write dependency. If it’s read/read dependency there’s no problem (because the value does not change). Have a look at the [Data Dependency article] (http://en.wikipedia.org/wiki/Data_dependency).
In your example, there’s no difference between the two codes,
mdepends oncandbbut these two are never written, so the compiler knows their value before getting into the loop. This is a called a read/read dependency, and it’s not a dependency itself.If you had written:
Then we would have a write/read dependency (we have to write in
aand then read froma, so we have to wait untilais calculated) and the optimization you did would be good.But once again, the compiler does this for you and many other things. It’s difficult to say that a micro-optimization in the high level code is gonna have a real impact in the assembler code, because maybe the compiler is doing that already for you, or maybe is reordering the code for you, or maybe is doing a thousand other things better than we can think of at first glance.
But anyway, it’s good just to know how things work under the carpet 🙂
Update to add some links
Have a look at these links to have a further understand about what the compiler can do to improve your code performance:
Loop unwinding
Dependency Analysis
Automatic parallelization
Vectorization