Repeat Until Belly.Full;
Those first 33% were easily gained. Let’s go for another round of SamplingProfiler:
Things are more satisfying: the line performing the actual work is now taking up most of the CPU time. Second comes the case of line. For further speed improvements, we now need to move the conditional out of the loop:
procedure DoSomething3(var data : array of Integer; what : TDoWhat); procedure RaiseUnsupported(what : TDoWhat); begin raise Exception.Create('Unsupported: '+GetEnumName(TypeInfo(TDoWhat), Integer(what))); end; var i : Integer; begin case what of dwInc : for i:=Low(data) to High(data) do Inc(data[i]); dwDec : for i:=Low(data) to High(data) do Dec(data[i]); else RaiseUnsupported(what); end; end;
We have increased the line count noticeably, but most of those extra lines are still cosmetic. What further makes it a reasonable trade-off is that the execution time has been reduced by 66% from the initial version, it now executes 3 times faster!
Are there any more easy gains to be had? Let’s run the last version through SamplingProfiler:
More than 92% of the execution time now goes to the loop and actual work. We got only a wee bit left for stack setup (line 96) and the case of (line 97). At this point, the above makes it clear that if you want to go faster you’ll have to increase the line count and code complexity significantly as you’ll need to replace the two-liner loops with something else, which is bound to be heavier (unrolling, SIMD, etc.)