473,394 Members | 1,761 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,394 software developers and data experts.

question on technique in .net

I find myself needing to loop through various collections in .NET a lot.
I am wondering if there is a generally accepted method for doing so.
I see three main ways (I'll use rows in a DataTable, which I am doing a
lot of work with)

1) For each curRow in curTable.Rows
2) For i = 0 to curTable.Rows.Count -1
currow = curtable.rows(i)
3) use curTable.Rows.GetEnumerator

Option 1 yields the shortest code, but which runs the fastest? I
suspect they are very similar, but I also suspect that someone out there
actually *knows*!

Thanks for any insights you have!

-Casey
Nov 21 '05 #1
4 1111
Using an enumerator is a nice clean way to do it, unfortunately, you cannot
change anything in the collection which might upset the loop. IE you cannot
remove the current item dependent on some condition, otherwise you will end
up with an exception.

As far as speed is concerned, this depends on the way the enumerator is
coded. I suspect that the differences will be quite small. If your that
worried about it, try it

Regards - OHM

"C Williams" <no****@thank.you> wrote in message
news:u0*************@tk2msftngp13.phx.gbl...
I find myself needing to loop through various collections in .NET a lot. I
am wondering if there is a generally accepted method for doing so. I see
three main ways (I'll use rows in a DataTable, which I am doing a lot of
work with)

1) For each curRow in curTable.Rows
2) For i = 0 to curTable.Rows.Count -1
currow = curtable.rows(i)
3) use curTable.Rows.GetEnumerator

Option 1 yields the shortest code, but which runs the fastest? I suspect
they are very similar, but I also suspect that someone out there actually
*knows*!

Thanks for any insights you have!

-Casey

Nov 21 '05 #2

Using "for each" is the same as using "GetEnumerator" -- the compiler
produces the GetEnumerator code for you behind the scenes.

Using for-each is generally recommended over an index loop because the
for-each loop may be able to loop through the collection more
efficiently and also the for-each loop will be more common as not all
collections are numerically indexed and thus an index loop will not
where everywhere a for-each loop is supported.

As for which really runs faster, it depends on the collection. In
your example, the index loop will run faster because the
for-each/GetEnumerator versions have the extra overhead of additional
conditionals and method calls, ensuring concurrency (nobody else
changed the list while you're looping), maintaining an internal index,
and maintaining a reference to both the index and the current value.
The speed difference is negligible though--0.003 seconds on 100,000
rows by my tests.

HTH,

Sam
On Thu, 03 Feb 2005 11:22:56 -0500, C Williams <no****@thank.you>
wrote:
I find myself needing to loop through various collections in .NET a lot.
I am wondering if there is a generally accepted method for doing so.
I see three main ways (I'll use rows in a DataTable, which I am doing a
lot of work with)

1) For each curRow in curTable.Rows
2) For i = 0 to curTable.Rows.Count -1
currow = curtable.rows(i)
3) use curTable.Rows.GetEnumerator

Option 1 yields the shortest code, but which runs the fastest? I
suspect they are very similar, but I also suspect that someone out there
actually *knows*!

Thanks for any insights you have!

-Casey


Nov 21 '05 #3
Casey,
Option 1 yields the shortest code, but which runs the fastest? I Which is faster really depends on the collection you are iterating over.

I normally use For Each as its the "cleanest" and "most obvious" what you
are doing.

Remember that most programs follow the 80/20 rule (link below) that is 80%
of the execution time of your program is spent in 20% of your code. I will
optimize the 20% once that 20% has been identified & proven to be a
performance problem via profiling (see CLR Profiler in my other message).

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware...timization.pdf

Hope this helps
Jay

"C Williams" <no****@thank.you> wrote in message
news:u0*************@tk2msftngp13.phx.gbl...I find myself needing to loop through various collections in .NET a lot. I
am wondering if there is a generally accepted method for doing so. I see
three main ways (I'll use rows in a DataTable, which I am doing a lot of
work with)

1) For each curRow in curTable.Rows
2) For i = 0 to curTable.Rows.Count -1
currow = curtable.rows(i)
3) use curTable.Rows.GetEnumerator

Option 1 yields the shortest code, but which runs the fastest? I suspect
they are very similar, but I also suspect that someone out there actually
*knows*!

Thanks for any insights you have!

-Casey

Nov 21 '05 #4
Thank you all for your input. Particularly for the 80/20 comment. I
have read similar things, but it's easy to forget and get sucked in to
trying to make every bit of code as efficient as possible.

Jay B. Harlow [MVP - Outlook] wrote:
Casey,
Option 1 yields the shortest code, but which runs the fastest? I


Which is faster really depends on the collection you are iterating over.

I normally use For Each as its the "cleanest" and "most obvious" what you
are doing.

Remember that most programs follow the 80/20 rule (link below) that is 80%
of the execution time of your program is spent in 20% of your code. I will
optimize the 20% once that 20% has been identified & proven to be a
performance problem via profiling (see CLR Profiler in my other message).

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware...timization.pdf

Hope this helps
Jay

"C Williams" <no****@thank.you> wrote in message
news:u0*************@tk2msftngp13.phx.gbl...
I find myself needing to loop through various collections in .NET a lot. I
am wondering if there is a generally accepted method for doing so. I see
three main ways (I'll use rows in a DataTable, which I am doing a lot of
work with)

1) For each curRow in curTable.Rows
2) For i = 0 to curTable.Rows.Count -1
currow = curtable.rows(i)
3) use curTable.Rows.GetEnumerator

Option 1 yields the shortest code, but which runs the fastest? I suspect
they are very similar, but I also suspect that someone out there actually
*knows*!

Thanks for any insights you have!

-Casey



Nov 21 '05 #5

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

Similar topics

17
by: Anthony Roberts | last post by:
If I end indentation levels with "pass" statements, will I piss off people that have to read my code? eg: for i in xrange(0,5): if i: print i pass print i * -1 pass
4
by: Robert Ferrell | last post by:
I have a style question. I have a class with a method, m1, which needs a helper function, hf. I can put hf inside m1, or I can make it another method of the class. The only place hf should ever...
2
by: Samuel Hon | last post by:
Hi All I'm creating some SPs and I've got a query which is inserting data into a table with a a unique constraint: CREATE TABLE ( IDENTITY (1, 1) NOT NULL , (50) COLLATE...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
2
by: Woody Splawn | last post by:
I have a question about technique with regard to filling drop downs. I have a Winform that has several different tabs and tables associated with it. In an effort to keep the load time small I...
30
by: zexpe | last post by:
I have an extremely cpu/data intensive piece of code that makes heavy use of the following function: void convertToDouble(const std::string& in, double& out) { out = atof(in.c_str()); } I...
0
by: BenWeber | last post by:
I have XML formed as follows (this is just a part of the XML) - <players> - <player id="814090"> <name>Brian</name> <surname>Bradford</surname> <countryfrom id="4">USA</countryfrom>...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
1
by: Ben Bacarisse | last post by:
cri@tiac.net (Richard Harter) writes: <snip> I too was going to mention the technique until I saw Eric's reply because in your sketch you said: | we have definitions like | | struct...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.