Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

forward-iteration vs reverse-iteration

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John A Grandy - 25 May 2008 01:30 GMT
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 ?
Peter Duniho - 25 May 2008 01:43 GMT
> [...]
> In other words, is there a performance advantage to pre-sorting a list  
> so that forward iteration may be used in implementing an algorithm ?

Highly unlikely.

There's a theoretical issue with respect to caching and paging, but as in  
previous answers: until you have demonstrated through particular  
performance measurements that there's an _actual_ performance problem, it  
doesn't make sense to rework your algorithms based on some presumed  
performance difference.

http://www.google.com/search?q=premature+optimization

Pete
Arne Vajhøj - 25 May 2008 03:15 GMT
> Is there a performance difference between forward iteration and reverse
> iteration through a List<string> ?
[quoted text clipped - 13 lines]
> 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
Doug Forster - 25 May 2008 21:32 GMT
Just to actually answer your question: Yes there could be(however small). If
you iterate backwards then the target end value is calculated only once at
the start, otherwise it will be evaluated each time. Neither can I see any
readability issue either way. Reverse iteration is a common technique if you
need to delete items as you go.

Cheers
Doug Forster
Ben Voigt [C++ MVP] - 28 May 2008 23:13 GMT
> Just to actually answer your question: Yes there could be(however
> small). If you iterate backwards then the target end value is
> calculated only once at the start, otherwise it will be evaluated
> each time. Neither can I see any readability issue either way.
> Reverse iteration is a common technique if you need to delete items
> as you go.

But "copy-with-exclusions" is faster than reverse iterating delete if the
number of items removed averages at least two (for uniformly distributed
items), and then the direction of traversal once again doesn't matter.

> Cheers
> Doug Forster

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.