473,671 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1613
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.MaxValu e
For Each item as Object In Collection
'do nothing - timing loop code
Next
Next
Console.WriteLi ne "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.mic rosoft.com> wrote in message
news:58******** *************** ***********@mic rosoft.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.MaxValu e
For Each item as Object In Collection
'do nothing - timing loop code
Next
Next
Console.WriteLi ne "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 "correctnes s" (OO) and "readabilit y" 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.Dis pose
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 QueryPerformanc eCounter 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******** *****@tk2msftng p13.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
3410
by: Max M | last post by:
There is a story today on Slashdot Open Source Project Management Lessons ====================================== http://developers.slashdot.org/article.pl?sid=03/07/02/1817220&mode=flat&tid=185 "Paul Baranowski takes a moment to reflect on Open Source Project Management in his blog. His reflections are based on the first two years of the Peek-a-booty project." Interesting comments on media coverage,
3
2824
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 > http://mail.python.org/mailman/listinfo/python-list > or, via email, send a message with subject or body > 'help' to
17
1665
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 my personal economy. I wrote it in Java because I wanted to learn the language for a course in programming at my university. Now that I have acquired an interrest in Python I was thinking about porting my program to Python.
7
3040
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 studio 2003 .net optimization were enhanced and that i must gain in performance if I switch to VS 2003 or intel compiler. So I send him the project and he returned a compiled DLL with VS 2003. Result : the VS 2003 compiled Dll is slower than the VC6...
6
3537
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 but the first access to the result set takes longer that to do the query in SQL and process the sql results. From my trace.axd LDAP Test Starting Search 0.000112 0.000043 LDAP Test Done Search 0.003821 0.003374 <--- fast query at .003 sec LDAP...
5
1085
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 u.GetViewBounds.Y + u.GetViewBounds.Height Dim Dx As Integer = x - u.Location.X Dim Dy As Integer = y - u.Location.Y If Dx * Dx + Dy * Dy <= u.ViewRange * u.ViewRange Then 'punctul e in cercul vizual
13
3024
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 for accessing objects, controls, etc. in VB/Access2003. In particular where will I find explanations of:- Actions, Functions, Methods, Properties - I'm understand the
10
1713
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, etc... The code is slow because it is processing large amounts of data many many times. However, is there a way to compile the code to other formats to make it
22
2701
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 things. So, in instances where I list, for example, the latest weblogs. I list the headline of the weblog, the date and the name of the member who wrote it. Now, the name isn't just "Smith", but rather Smith's online status, his nick and his...
0
1961
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 reaches a point 0 (compare a hand spinning a fortune wheel). now, this is quite an easy this to achieve, since you just have to set an interval to increase rotation, with a value that decreases over time (on every call), till it finally reaches a...
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.