I gave the code a quick performance test, using Snippet Compiler. The code I used is as follows:
public static void Main()
{
DateTime datStart = DateTime.UtcNow;
const int timesToLoop = 1000000;
for (int i=0; i < timesToLoop; i++)
{
WL("Line Number " + i.ToString());
}
DateTime datEnd = DateTime.UtcNow;
TimeSpan tsTimeTaken = datEnd - datStart;
WL("Time Taken: " + tsTimeTaken.TotalSeconds);
RL();
}
Note, WL and RL are simply helper methods to read and write to the console.
To test the non-constant version, I simply removed the const
keyword. The results were surprising:
Time Taken (average of 3 runs)
Using const keyword 26.340s
Without const keyword 28.276s
I'm aware that this is very rough'n'ready test, but the use of the const
keyword appears to count as a valid micro-optimization.