Performance Question working with DataRows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SyraJM
    New Member
    • Oct 2008
    • 1

    Performance Question working with DataRows

    Which is the more efficient code?

    Code:
        Private Sub Test()
            For Each row As DataRow In dtMyTable.Rows
                Debug.Print("{0}", row(0))
            Next
        End Sub
    
        Private Sub Test2()
            If dtMyTable.Rows.Count > 0 Then
                For Each row As DataRow In dtMyTable.Rows
                    Debug.Print("{0}", row(0))
                Next
            End If
        End Sub
    Looking at the IL, it takes roughly twice as many lines to involve the enumerator as it does to query the RowCount. So does this mean that we should base our decision on the likelihood of the table to contain rows? If it's likely to be empty then using the If Block would provide an earlier exit.

    I'm new at reading IL. It appears that if the table contains some rows, two instances of the dataTable is created in the Test2 method. One to check the RowCount and another to invoke the enumerator logic. Does the IL reuse these instances?

    I know this might be really splitting hairs but I'd be interested in the best choice for optimum performance. Thanks for your thoughts.
Working...