Using temp data in a loop [modified]
-
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
NextDim row As DataRow = Nothing
For i As Integer = 0 To costTable.Rows.Count - 1
row = costTable.Rows(i)
'use the data in the row
Nextmodified on Saturday, August 23, 2008 2:08 PM
-
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
NextDim row As DataRow = Nothing
For i As Integer = 0 To costTable.Rows.Count - 1
row = costTable.Rows(i)
'use the data in the row
Nextmodified on Saturday, August 23, 2008 2:08 PM
-
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
NextDim row As DataRow = Nothing
For i As Integer = 0 To costTable.Rows.Count - 1
row = costTable.Rows(i)
'use the data in the row
Nextmodified on Saturday, August 23, 2008 2:08 PM
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
NextDespite everything, the person most likely to be fooling you next is yourself.
-
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
NextDespite everything, the person most likely to be fooling you next is yourself.
-
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?
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