Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. IT & Infrastructure
  4. For loop

For loop

Scheduled Pinned Locked Moved IT & Infrastructure
question
6 Posts 4 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Md Marufuzzaman
    wrote on last edited by
    #1

    Which for loop is more faster And Why? Can anyone suggest.. (i)     Foreach I in Items                   //Your code             Next I (ii)     For int   Counter =   0 ; Counter <ItemArray.Len() ; Counter++ //Your code

    I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.


    Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman

    L P 2 Replies Last reply
    0
    • M Md Marufuzzaman

      Which for loop is more faster And Why? Can anyone suggest.. (i)     Foreach I in Items                   //Your code             Next I (ii)     For int   Counter =   0 ; Counter <ItemArray.Len() ; Counter++ //Your code

      I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.


      Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, it depends on the kind of collection. Could be faster, equal or slower. But never by much IMO. My advice is to write it in the way you feel most comfortable with. e.g. a foreach(int x in intArray) gets actually compiled exactly the same as a for(int x=0; x<intArray.Length; x++). others really call GetEnumerator, which (should) create an object that holds the current state of the enumeration and protect you against the collection being changed while enumerating. So they are not semantically identical. BTW: having ItemArray.Len() in the exit condition can be wasteful as it will be recalculated everytime, it might have been changed by some code somewhere. Better use a local variable (or a foreach!). If you have a specific case in mind, what is keeping you from: 1. performing a test run? 2. looking at the code (e.g. with Reflector)? :)

      Luc Pattyn

      :badger: :jig: :badger:

      Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

      :jig: :badger: :jig:

      1 Reply Last reply
      0
      • M Md Marufuzzaman

        Which for loop is more faster And Why? Can anyone suggest.. (i)     Foreach I in Items                   //Your code             Next I (ii)     For int   Counter =   0 ; Counter <ItemArray.Len() ; Counter++ //Your code

        I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.


        Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        For an array it may not matter. For other collections it could go either way. Try both with the particular collection and see. Bear in mind that when using foreach, you're not allowed to alter the collection. If the collection might change as you're enumerating it, for (or while) may be your only choice. And as Luc mentioned, you may want to store the length in a local variable if retrieving it is costly and it doesn't change. Another option, if the order isn't important, is to run the for loop "backward": for ( int i = a.Len() - 1 ; i >= 0 ; i-- ) ...

        M 1 Reply Last reply
        0
        • P PIEBALDconsult

          For an array it may not matter. For other collections it could go either way. Try both with the particular collection and see. Bear in mind that when using foreach, you're not allowed to alter the collection. If the collection might change as you're enumerating it, for (or while) may be your only choice. And as Luc mentioned, you may want to store the length in a local variable if retrieving it is costly and it doesn't change. Another option, if the order isn't important, is to run the for loop "backward": for ( int i = a.Len() - 1 ; i >= 0 ; i-- ) ...

          M Offline
          M Offline
          Md Marufuzzaman
          wrote on last edited by
          #4

          Thanks for your clarification…:)

          I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.


          Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman

          Z 1 Reply Last reply
          0
          • M Md Marufuzzaman

            Thanks for your clarification…:)

            I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.


            Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman

            Z Offline
            Z Offline
            zahedonline
            wrote on last edited by
            #5

            According to me the second (ii) forloop is faster than foreach.. because each time for loop checks the count and dosn't get the whole object, it just fix a index and navigate then takes a appropriate values from the bject collection. So as per the performance wise For loop is far better than foreach. :)

            ZAK

            M 1 Reply Last reply
            0
            • Z zahedonline

              According to me the second (ii) forloop is faster than foreach.. because each time for loop checks the count and dosn't get the whole object, it just fix a index and navigate then takes a appropriate values from the bject collection. So as per the performance wise For loop is far better than foreach. :)

              ZAK

              M Offline
              M Offline
              Md Marufuzzaman
              wrote on last edited by
              #6

              Your explanation is logical...Actually Im also agree with that point. :thumbsup: :)

              I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.


              Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups