473,379 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

forward-iteration vs reverse-iteration

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 ?

Jun 27 '08 #1
4 3630
On Sat, 24 May 2008 17:30:00 -0700, John A Grandy
<johnagrandy@g-mail-dot-comwrote:
[...]
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
Jun 27 '08 #2
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

Jun 27 '08 #3
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

Jun 27 '08 #4
Doug Forster wrote:
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

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
1
by: qazmlp | last post by:
Is it a good style to re-forward declare the types, which were already forward declared in base header file, in derived class header file? // base.h class addrClass; class base { public:...
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
5
by: Simon Elliott | last post by:
For some time I've been using typedefs for STL containers for reasons outlined in: http://www.gotw.ca/gotw/046.htm However a major downside to this is that you can't forward declare a typedef...
5
by: Todd Huish | last post by:
I have noticed something disturbing when retrieving datasets over a relatively slow line (multiple T1). I am looking at about 25 seconds to retrieve 500 rows via a php-odbc link. This same select...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
7
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
4
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 I have a DB2 online DB backup that was done w/ the INCLUDE LOGS option. I am interested in restoring that backup, and rolling forward ONLY the logs contained in the...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.