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. Visual Basic
  4. Using temp data in a loop [modified]

Using temp data in a loop [modified]

Scheduled Pinned Locked Moved Visual Basic
performancetutorialquestion
5 Posts 3 Posters 1 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.
  • N Offline
    N Offline
    Netblue
    wrote on last edited by
    #1

    On the memory side of things, is it better to use the second example versus the first one, or does it not really matter in the end? How is memory allocated in the two situations? Will the first one create a ton of objects that get cleaned up after they drop out of scope? Will this affect memory and garbage collection?

    For i As Integer = 0 To costTable.Rows.Count - 1
    Dim row As DataRow = costTable.Rows(i)
    'use the data in the row
    Next

    Dim row As DataRow = Nothing
    For i As Integer = 0 To costTable.Rows.Count - 1
    row = costTable.Rows(i)
    'use the data in the row
    Next

    modified on Saturday, August 23, 2008 2:08 PM

    X G 2 Replies Last reply
    0
    • N Netblue

      On the memory side of things, is it better to use the second example versus the first one, or does it not really matter in the end? How is memory allocated in the two situations? Will the first one create a ton of objects that get cleaned up after they drop out of scope? Will this affect memory and garbage collection?

      For i As Integer = 0 To costTable.Rows.Count - 1
      Dim row As DataRow = costTable.Rows(i)
      'use the data in the row
      Next

      Dim row As DataRow = Nothing
      For i As Integer = 0 To costTable.Rows.Count - 1
      row = costTable.Rows(i)
      'use the data in the row
      Next

      modified on Saturday, August 23, 2008 2:08 PM

      X Offline
      X Offline
      xx77abs
      wrote on last edited by
      #2

      I'm not sure, but I think that it doesn't matter ... That they're the same ...

      1 Reply Last reply
      0
      • N Netblue

        On the memory side of things, is it better to use the second example versus the first one, or does it not really matter in the end? How is memory allocated in the two situations? Will the first one create a ton of objects that get cleaned up after they drop out of scope? Will this affect memory and garbage collection?

        For i As Integer = 0 To costTable.Rows.Count - 1
        Dim row As DataRow = costTable.Rows(i)
        'use the data in the row
        Next

        Dim row As DataRow = Nothing
        For i As Integer = 0 To costTable.Rows.Count - 1
        row = costTable.Rows(i)
        'use the data in the row
        Next

        modified on Saturday, August 23, 2008 2:08 PM

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        The generated code for the both examples will be exactly the same, except that the variable will be assigned a null value before the loop in the second example. The variable is allocated on the stack, which is very cheap. No objects are created, so there is no garbage collection involved at all. The scope of the variable differs between the examples, but that only matters to the compiler. All local variables are always allocated when the stack frame for the method is created, regardless of where in the method the variables are declared. Unless you need the counter variable for something else, I would recommend that you use ForEach instead:

        ForEach row As DataRow in costTable.Rows
        'use the data in the row
        Next

        Despite everything, the person most likely to be fooling you next is yourself.

        N 1 Reply Last reply
        0
        • G Guffa

          The generated code for the both examples will be exactly the same, except that the variable will be assigned a null value before the loop in the second example. The variable is allocated on the stack, which is very cheap. No objects are created, so there is no garbage collection involved at all. The scope of the variable differs between the examples, but that only matters to the compiler. All local variables are always allocated when the stack frame for the method is created, regardless of where in the method the variables are declared. Unless you need the counter variable for something else, I would recommend that you use ForEach instead:

          ForEach row As DataRow in costTable.Rows
          'use the data in the row
          Next

          Despite everything, the person most likely to be fooling you next is yourself.

          N Offline
          N Offline
          Netblue
          wrote on last edited by
          #4

          Thanks for the excellent explanation. I understand there are certain precautions to using a foreach, sometimes it can create overhead from an enumerator. Is there an easy way to know the difference?

          G 1 Reply Last reply
          0
          • N Netblue

            Thanks for the excellent explanation. I understand there are certain precautions to using a foreach, sometimes it can create overhead from an enumerator. Is there an easy way to know the difference?

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            The ForEach loop uses an enumerator, however the enumerator is usually a struct so it's allocated on the stack. The performance difference between a ForEach loop and using a counter is quite small, and it depends on the situation whichever is faster. Unless you have an actual performace problem, you should just use the one that best fits your needs.

            Despite everything, the person most likely to be fooling you next is yourself.

            modified on Monday, August 25, 2008 6:21 PM

            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