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. General Programming
  3. C#
  4. ForLoop & Foreach

ForLoop & Foreach

Scheduled Pinned Locked Moved C#
performance
4 Posts 3 Posters 0 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.
  • S Offline
    S Offline
    SysJey
    wrote on last edited by
    #1

    Performacne(Speed) wise which one is best among For loop and foreach Jey

    C 1 Reply Last reply
    0
    • S SysJey

      Performacne(Speed) wise which one is best among For loop and foreach Jey

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      They exist to service different needs. However, in some situations you can use both. for(int i=0; i<array.Length; i++) is faster for iterating over an array, but then you have to do more work as you have to extract the values out of the array manually.


      "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog -- modified at 4:34 Monday 29th May, 2006

      S 1 Reply Last reply
      0
      • C Colin Angus Mackay

        They exist to service different needs. However, in some situations you can use both. for(int i=0; i<array.Length; i++) is faster for iterating over an array, but then you have to do more work as you have to extract the values out of the array manually.


        "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog -- modified at 4:34 Monday 29th May, 2006

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        Actually, Brad Abrams here[^] says that for arrays, for and foreach generate identical code for iteration.

        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        C 1 Reply Last reply
        0
        • S S Senthil Kumar

          Actually, Brad Abrams here[^] says that for arrays, for and foreach generate identical code for iteration.

          Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Interesting - I've never check the MSIL but I've seen performance stats on code that suggest the for() is faster than foreach() for arrays - I guess it was talking about ArrayList and I didn't pick up on that.


          "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

          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