469,167 Members | 1,979 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,167 developers. It's quick & easy.

Performance Question working with DataRows

1
Which is the more efficient code?

Expand|Select|Wrap|Line Numbers
  1.     Private Sub Test()
  2.         For Each row As DataRow In dtMyTable.Rows
  3.             Debug.Print("{0}", row(0))
  4.         Next
  5.     End Sub
  6.  
  7.     Private Sub Test2()
  8.         If dtMyTable.Rows.Count > 0 Then
  9.             For Each row As DataRow In dtMyTable.Rows
  10.                 Debug.Print("{0}", row(0))
  11.             Next
  12.         End If
  13.     End Sub
  14.  
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.
Oct 3 '08 #1
0 885

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by bjarne | last post: by
5 posts views Thread by Nathan Sokalski | last post: by
9 posts views Thread by Brad | last post: by
4 posts views Thread by NormD | last post: by
3 posts views Thread by creator_bob | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.