472,145 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

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 926

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
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.