I have recently begun to think about optimisation, now I know there are many books and articles but I have a particular scenario i’m interested in.
A.
for (i = 0; i < limit + 5; i++)
cout << test;
B.
limit2 = limit +5;
for (i = 0; i < limit2; i++)
cout << test;
What I want to know is would the second piece of code run faster because it only has to do the mathematical calculation once or is the calculation stored for the lifetime of the loop.
You’re focusing on an optimization that almost certainly doesn’t matter, regardless of how it gets compiled. Focus on picking the right algorithms and design for the rest of your code. Profile to find out what’s slow and optimize the areas that take the most time. Typically 10% of the code takes 90% of the time. You’re currently thinking about a very small part of the 90% of the code that takes far less than 10% of the time in just about every possible scenario beyond having an empty loop block.