Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Is foreach slow?

Posted on 2012-09-07
Medium Priority
Last Modified: 2012-09-17
If i have a collection, and use foreach loop to go thru it, is that slow? someone on the team said foreach is slow and there's a better way of doing it (not sure what way that is tho)
Question by:Camillia
LVL 17

Assisted Solution

bigeven2002 earned 500 total points
ID: 38378448

Although I am not an expert in ASP, I will give it a shot.  It really depends on what is going on in the foreach loop.  Does it involve one or more IF statements within the loop?  Multiple IF statements can slow it down as each one has to be processed regardless.

If you are using IF statements within the loop to check for different values of the same variable, then you can use a switch statement to speed that up.  Kind of similar to this:


Other than that, I am honestly not sure what could be faster than that since my experience with ASP.net is weak.
LVL 58

Assisted Solution

Gary earned 500 total points
ID: 38378501
foreach is the recommended way to do it. It is after all a loop - what other way can you do a loop.  You could maybe use a repeater which looks cleaner but is actually worse.
But ultimately it depends on what the data is, how you are using it etc. e.g. you don't loop a thousand times to find one value.
LVL 20

Accepted Solution

informaniac earned 500 total points
ID: 38378961
foreach(GridViewRow gvRow in gv1.Rows)


GridViewRow gvRow;
for(int i = 0; i < gv1.Rows.Count; i++)
      gvRow = gv1.Rows[i];

Open in new window

LVL 26

Assisted Solution

by:Alan Warren
Alan Warren earned 500 total points
ID: 38380797
Yes, I reckon it is/possibly could be slower, of course it depends on the type of 'each'. If each 'each' is an integer, wouldn't make much difference.

If you were doing:
  for each ctl as control in me.controls
  ' the ctl is instantiated, takes resources, especially if it's a complex control.
  ' you can verify that by using 'With' to expose the properties of the ctl inside the for each loop.
  With ctl
    .text = "hello world"
  End With

On the other hand, if you were looping like:
  for i = 0 to me.controls.count
  ' then the control is not instantiated until you say so, all you have instantiated is an integer array().

Alan ";0)

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question