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

About speed with different looping-techniques?

I have a Dataset with one table in it.

If I need to loop thru that table I know of two ways that I would choose..

1. Do a "For Each DataRow in Datatable"
2. You create an IEnumerator fron the Table.Rows object. And loop thru that
one (a bit more code needed).

Questions:

1. Which one would be fastest and Why?

2.In general... how do you actually measure a thing like that, are there a
very precise way of measuring or any guidlines that you can use in general
to figure out what techniques you should choose in different situations?
Best Regards/
Lars Netzel
Nov 20 '05 #1
3 1600
From a real quick test, it appears that managing the IEnumerator CAN be quicker, but I had to run through a few million loops to feel it. This makes since when you look at what a For Each loop is doing - creating the IEnumerator on the fly and cycling over it.

How do you measure something like that? By doing it so many times that you can see it affects. I normally do something like (typed here so beware typos):

Dim StartTime as DateTime

StartTime = DateTime.Now
For i as integer = 0 to Integer.MaxValue
For Each item as Object In Collection
'do nothing - timing loop code
Next
Next
Console.WriteLine "Took " & DateTime.Now - StartTime & " seconds."

HTH
--
David Williams, VB.NET MVP
"Lars Netzel" wrote:
I have a Dataset with one table in it.

If I need to loop thru that table I know of two ways that I would choose..

1. Do a "For Each DataRow in Datatable"
2. You create an IEnumerator fron the Table.Rows object. And loop thru that
one (a bit more code needed).

Questions:

1. Which one would be fastest and Why?

2.In general... how do you actually measure a thing like that, are there a
very precise way of measuring or any guidlines that you can use in general
to figure out what techniques you should choose in different situations?
Best Regards/
Lars Netzel

Nov 20 '05 #2
In addition, be aware, that you cannot remove rows during an enumerated loop
or you will end up with an exception.

If you do need to alter the contents that way, use a backwards counting
for/next loop

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"David Williams" <Da***********@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
From a real quick test, it appears that managing the IEnumerator CAN be quicker, but I had to run through a few million loops to feel it. This
makes since when you look at what a For Each loop is doing - creating the
IEnumerator on the fly and cycling over it.
How do you measure something like that? By doing it so many times that you can see it affects. I normally do something like (typed here so beware
typos):
Dim StartTime as DateTime

StartTime = DateTime.Now
For i as integer = 0 to Integer.MaxValue
For Each item as Object In Collection
'do nothing - timing loop code
Next
Next
Console.WriteLine "Took " & DateTime.Now - StartTime & " seconds."

HTH
--
David Williams, VB.NET MVP
"Lars Netzel" wrote:
I have a Dataset with one table in it.

If I need to loop thru that table I know of two ways that I would choose..
1. Do a "For Each DataRow in Datatable"
2. You create an IEnumerator fron the Table.Rows object. And loop thru that one (a bit more code needed).

Questions:

1. Which one would be fastest and Why?

2.In general... how do you actually measure a thing like that, are there a very precise way of measuring or any guidlines that you can use in general to figure out what techniques you should choose in different situations?
Best Regards/
Lars Netzel

Nov 20 '05 #3
Lars,
In addition to the other comments.

Does it really matter?
I find its better to code for "correctness" (OO) and "readability" first,
then code for performance only when a routine has proven to have performance
problems via profiling.

IMHO: For Each is much more readable then the IEnumerator loop, plus For
Each is more "correct" in that the For Each will call IEnumerator.Dispose
for me if needed...
Also as David stated, the For Each is implemented in terms of IEnumerator,
there is not going to be any real difference in speed.
In addition to David's sample of using DateTime.Now to time code, I normally
use QueryPerformanceCounter to time code.

http://support.microsoft.com/default...b;en-us;306978

Hope this helps
Jay

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:ua*************@tk2msftngp13.phx.gbl...
I have a Dataset with one table in it.

If I need to loop thru that table I know of two ways that I would choose..

1. Do a "For Each DataRow in Datatable"
2. You create an IEnumerator fron the Table.Rows object. And loop thru that one (a bit more code needed).

Questions:

1. Which one would be fastest and Why?

2.In general... how do you actually measure a thing like that, are there a
very precise way of measuring or any guidlines that you can use in general
to figure out what techniques you should choose in different situations?
Best Regards/
Lars Netzel

Nov 20 '05 #4

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

Similar topics

22
by: Max M | last post by:
There is a story today on Slashdot Open Source Project Management Lessons ======================================...
3
by: fdsl ysnh | last post by:
--- python-list-request@python.orgдµÀ: > Send Python-list mailing list submissions to > python-list@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit >...
17
by: Jan Danielsson | last post by:
Hello all, I recently started using Python, and I must say I like it. Both the language and libraries available for it. Background: I have written an application which I use to keep track of...
7
by: YAZ | last post by:
Hello, I have a dll which do some number crunching. Performances (execution speed) are very important in my application. I use VC6 to compile the DLL. A friend of mine told me that in Visual...
6
by: cameron | last post by:
I have always been under the impression that LDAP was optimized for speed. Fast queries, fast access, slower writes. I have a block of data in LDAP and in SQL. Exact same data. The query is fast...
5
by: Crirus | last post by:
What do you think about this approaches, wich one is the best? For x As Integer = u.GetViewBounds.X To u.GetViewBounds.X + u.GetViewBounds.Width For y As Integer = u.GetViewBounds.Y To...
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
10
by: Ali Chambers | last post by:
Hi, I've written a programme that processes stock market price data in VB.NET 2005 Express. I've optimised the code as much as possible, eg:- using arrays, not passing parameters to functions,...
22
by: Sandman | last post by:
So, I have this content management system I've developed myself. The system has a solid community part where members can register and then participate in forums, write weblogs and a ton of other...
0
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...

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.