Is foreach slow?

Posted on 2012-09-07
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


    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 is weak.
    LVL 58

    Assisted Solution

    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

    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
    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now