John A Grandy wrote:
Is there a performance difference between forward iteration and reverse
iteration through a List<string?
for ( i = 0; i < myList.Count; i++ )
{
// do work, such as forward iterate through a subset of myList ...
}
vs
for ( j = myList.Count - 1 ; j >= 0; j-- )
{
// do work, such as reverse iterate through a subset of myList ...
}
In other words, is there a performance advantage to pre-sorting a list
so that forward iteration may be used in implementing an algorithm ?
Stuff like that was very relevant when coding in C about
20-30 years ago.
Today is a completely waste of time.
Most likely your gains from that kind of optimization will
be so small that you can not even measure them in your app.
Even if there were some gains then spending 10 USD extra
on a few hundrded MHz extra would be much more cost efficient
than you spending time on it.
Oh - and even though one way is faster on your CPU with your
version of .NET, then there are absolutely no guarantee that
it will be the case on other.
If it is a hobby like old World War I biplanes, then start
measuring the times yourself.
If it is actual work, then focus on writing readable code
with a healthy architecture and create some value that way.
Arne