Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataset iteration effeciency

Status
Not open for further replies.

cjburkha

Programmer
Jul 30, 2004
76
US
Hi,

Often I have to loop through datasets, and I am wondering what is the best way to do it. I can do like this

Code:
dim count as integer = 0
dim numRows = mySet.Tables(0).Rows.count
while count < numRows
   ''code here
    count += 1
end while
or
Code:
dim count as integer = 0
while count <  mySet.Tables(0).Rows.count
   ''code here
    count += 1
end while
Code:
dim myRow as system.data.datarow
  for each myRow in linksDS.tables(0).rows
    ''code here
   next myRow

Are any of these methods better than the other, faster?

Or to phrase it another way, if I know I am going to be using a class property, am I better off dumping that value in a variable, or is accessing that property just as fast as the variable.

Does this
Code:
dim numRows as integer = dataset.tables(0).rows.count
while count < numRows
end while
have any advantage over
Code:
while count < dataset.tables(0).rows.count
end while

I'm concerned that count is not actually a static variable, but that each time I am accessing the property count it is calling a function get that iterates the rows collection to see how many there are.

I hope I spelled out my question clearly, I look forward to your opinions.
 
Why not code it all three ways, then use ildasm.exe to look at the MSIL code that gets generated to see which results in fewer instructions?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the good idea. I did what you suggested, and was a little surprised.

The 0 variable method (for each) has 76 instructions, the 1 variable method (count < dataset.tables(0).rows.count) has 87 instructions, and the 2 variable method( count < numrows) has 91 instructions. I was expecting an increase in performance of 27% in the 0 variable method over the 2 variable method. I made a test page, and instead found a 64% increase in the 0 variable method over the 2 variable method.

The bottom line is, for me, for each is much faster, both in cycles and on my fingers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top