473,796 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop performance

I've been told that how one does a loop in VB .NET cna have significant speed
differernces. For example:

dim therows() as datarow
.....
dim row as datarow

for each row in therows
.....
next row
------------------------
vs.
-----------------------
dim therows() as datarow
.....
dim i as integer

for i = o to therows.getuppe rbound(0)
.....
next i

with the classic "for i=1 to next i" being much slower. Some testing on
really large string arrays confirms this. But we've heard otherwise, too.
And what about "while" loops, other ways of looping?

I suppose, too, it could have something to do with how data in an array,
rows in a dataset, etc, are indexed, not just how the counter gets
incremented.

Any articles, etc. on this?
Nov 21 '05 #1
4 1186
NormD,

Forget it about "significan t speed differernces" if it is not really about
very long strings.

If we are talking about by instance datarows than it are parts of
nanoseconds. If it is in an UI application, than the paint on the window
from one label will with a normal sized table cost probably 1000000 times
more.

If it are strings have than a look what you do and if needed because you see
longtime operations, try to use the stringbuilder class. A string is
immutable and therefore has to be created new if there is done any change in
it. (Although there are some optimisations done in some methods so it is not
forever true).

I hope that this gives an idea.

Cor
Nov 21 '05 #2
Hi

Based on my test, it seems to be as the same speed as the other one
quantitatively.
NOTE: please do the test in long interval to elimitate the other effects.
e.g.
the form1_load will take a lot of time and memory to initialize.

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim od As Long = DateTime.Now(). Ticks
For i As Integer = 0 To strs.GetUpperBo und(0)
s = strs(i)
Next
Button1.Text = (DateTime.Now() .Ticks - od).ToString()
End Sub
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
Dim od As Long = DateTime.Now(). Ticks
For Each ss As String In strs
s = ss
Next
Button2.Text = (DateTime.Now() .Ticks - od).ToString()
End Sub
Dim strs(1024 * 1024 * 10) As String
Dim s As String
Dim rd As New Random
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Me.SqlDataAdap ter1.Fill(Me.Da taSet11)
'dt = Me.DataSet11.Ta bles(0)
For i As Integer = 0 To strs.GetUpperBo und(0)
strs(i) = rd.Next().ToStr ing()
Next
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #3
Thanks, guys.

On one hand I'd tend to agree with you that the difference should be very
small at most. On the other hand the person who discussed it with me is no
dummy and insists that the way they loop through their data makes a
substantial difference; ptactice, not theory. I will see if I can get the
specific example; I'm wondering is there is something else besides the loop
being changed, and that's what's making the diffference.

""Peter Huang" [MSFT]" wrote:
Hi

Based on my test, it seems to be as the same speed as the other one
quantitatively.
NOTE: please do the test in long interval to elimitate the other effects.
e.g.
the form1_load will take a lot of time and memory to initialize.

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim od As Long = DateTime.Now(). Ticks
For i As Integer = 0 To strs.GetUpperBo und(0)
s = strs(i)
Next
Button1.Text = (DateTime.Now() .Ticks - od).ToString()
End Sub
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
Dim od As Long = DateTime.Now(). Ticks
For Each ss As String In strs
s = ss
Next
Button2.Text = (DateTime.Now() .Ticks - od).ToString()
End Sub
Dim strs(1024 * 1024 * 10) As String
Dim s As String
Dim rd As New Random
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Me.SqlDataAdap ter1.Fill(Me.Da taSet11)
'dt = Me.DataSet11.Ta bles(0)
For i As Integer = 0 To strs.GetUpperBo und(0)
strs(i) = rd.Next().ToStr ing()
Next
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #4
Hi

Ok, you are welcomed.
As for an Array, it is just a block of memory, so the index access is the
best approach, and the for each will implement the IEnumerator interface
for the Array class, and to the array class the concrete implement inside
should be the index access which is the same access mechanism.

On the other hand, in the Data Structure, another data structure is List,
which is a lots of seperated block memory, in this way, the IEnumerator
will win, because it just navigate through the List one by one, but use
Index will cause it repeated to access to the List.
e.g.
M1--->M2---->M3

This is a List.
What IEnumerator implement do is
1. M1
2. M2
3. M3

But for Index access, it may be as below.
1.M1
2. M1-->M2
3. M1-->M2-->M3

which is much slower.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #5

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

Similar topics

47
12344
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) { int result = 0; for(int i = 1;i <=iterations;i++) { result += i;
3
3066
by: pertheli | last post by:
Hello, I have a large array of pointer to some object. I have to run test such that every possible pair in the array is tested. eg. if A,B,C,D are items of the array, possible pairs are AB, AC, AD, BC and CD. I'm using two nested for loop as below, but it is running very slow whenever I access any data in the second loop.
10
14355
by: Bhan | last post by:
I heard for(i=0;i<20;i++) { do-something; } Can be optimized. Is this can really optimized by an equivalent for loop or with while or do while loops?
8
3017
by: Dave Veeneman | last post by:
In a for-loop, is a calculated expression re-calculated on each pass through the loop, or only once, when the loop is initialized? For example, assume the following loop: for (int i = 0; i < myArray.Length - 1; i++) { // code here } Is "myArray.Length - 1" calculated once, or on each pass through the loop?
15
2678
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a "foreach" loop instead of a "for" loop. This made absolutely no sense to me, but the author had posted his code and timing results. The "foreach" (a VB and other languages construct) was 0.01 seconds to access 1000 lines in rich text box,...
3
2007
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop, because GetEnumerator always returns a new one. My proposal would allow the following scenario: You could for example the first and the last element of the list as a special case:
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10244
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
10201
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,...
1
7558
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
6802
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
5454
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.