To do alignment use
{$CodeAlign Loop=$20 Proc=$20}
Mind you, with all the "smart buildin micro optimizations" of modern cpu, in most cases you will not see a difference.
But, I have personally seen some of the few cases, where it made a difference. And a noticeable 20%.
If you put each loop into a proc of its own then you can:
- Do some warm up runs, where you discard the times
- Run each loop several times getting fastest and avg times (e.g. doing 20 runs for each bench)
- For more complex scenarios,
- reorder the function in code (in case it affects caching - code or data / well yours don't have data to cache)
- change the order in which you test them
Also you did not specify which optimization you compiled with. And the exact version of FPC. That can change results.
As for loop, the following are equal (the begin end are optional, but they don't generate code / at least they should not)
You could play with = vs > vs <> ...
for i := 0 to maxint-1 do begin
end;
i := 0; while i < maxint do begin
inc(i);
end;
i := 0; repeat
inc(i);
until i = maxint;
Do those you can then add any other workload such as your timing condition.
However, your check for the time will likely take much more time than the loop iteration itself. So you will not see any difference. If actually there is one.
But further more, the compiler optimizes in some cases the result of the loop and contained code. So which loop is faster may change depending on what you actually do in the loop.
One thing can be told, if the upper bound (in your case maxint) was something like "SomeContainer.Count - 1" and if calling Count was slow, then the for loop will be faster. Simply because it calls count only once, and caches the result.
On the other hand, for the same reason the for loop will fail, if the count changes during the loop.
As I said in each version of FPC the loops may compare differently.
And as the compiler gets better at optimizing there likely will be less of a difference (if there is any to start with).
That is except for the functional differences:
- for: only one evaluation of the upper bound
- repeat: no check before the first iteration
E.g. if you have an inner loop, that is being STARTED a million times, but each time only runs between 1 and 3 iterations, and you know it will always be at least 1, then when you use repeat, you have one comparison less. One for each of the million times that it gets started.
However, for that to make a difference, the loop itself must not take much time in the iterations. Because 1 comparison is hardly any time. And if the loop spend much time doing something inside it, then you still only save 0.00001%.