Code Optimisation in .Net
Saw an article a few months ago about code optimisation in .Net. The examples were in C# but I assume they apply to VB.Net and managed C++.
It concerned the widely known good practice of extracting a value into a local variable rather than hitting an object property repeatedly which incurs greater overhead. Such as the following code which extracts an array length into a variable rather than on each pass of the for loop:
int length = myStrings.Length;
for (int i = 0; i < length; i++)
{
string aString = myStrings[i];
}
Fair enough. This looks like good practice backed up by common sense. A good combination.
Now it seems that this is the wrong way. Because of .Net optimisation, the faster code gets the length property of the array in the loop:
for (int i = 0; i < myStrings.Length; i++)
{
string aString = myStrings[i];
}
So do I unlearn a coding pattern that I've used for years? No thanks - I'll carry on the old way.