question on technique in .net

  • Thread starter Thread starter C Williams
  • Start date Start date
C

C Williams

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
 
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
 
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
 
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/yetOptimization.pdf

Hope this helps
Jay
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top