-1

Which code is more efficient?

Dim drPerson As DataRow
Dim cCLS as new myClass
For Each drPerson In dtData.Rows
    Dim dsPers As New DataSet
    dsPers = cCLS.Get_PersonByID(drPerson("person_id"))
    ' Code block goes here to do something with dataset object
Next

or

Dim drPerson As DataRow
Dim dsPers As New DataSet
Dim cCLS as new myClass

For Each drPerson In dtData.Rows
    dsPers = cCLS.Get_PersonByID(drPerson("person_id"))
    ' Code block goes here to do something with dataset object
Next
djv
  • 15,168
  • 7
  • 48
  • 72
  • Did you try timing it yourself? You could loop over these code snippets 1,000,000 times, and see which one finishes faster. My guess is that there won't be a significant enough difference to matter. The [source](https://referencesource.microsoft.com/#System.Data/fx/src/data/System/Data/DataSet.cs,94665cdea302b675) for DataSet shows there is some code in the default constructor, so perhaps your second example is slightly faster, simply because the code in that constructor doesn't have to be executed each time through the loop. – Sean Skelly Apr 07 '20 at 20:00
  • I would start by getting rid of the first initialization (i.e., use `As DataSet` instead of `As New DataSet`) because it's redundant. Then, whether you declare `dsPers` before the loop or inside its body should be decided based on the scope where you need it to be accessible. – 41686d6564 stands w. Palestine Apr 07 '20 at 20:05
  • @Joel For more, see: [Declaring a variable inside or outside an foreach loop: which is faster/better?](https://stackoverflow.com/q/1884906/8967612) – 41686d6564 stands w. Palestine Apr 07 '20 at 20:11
  • You should use the first option, so as to keep the scope of the variable as narrow as possible. I believe that, in a Release build, the compiler will optimise the code so that it ends up like the second anyway. That means that you get the best of both worlds, i.e. you can only use the variable where you need it but the compiled application only has to create it once. – jmcilhinney Apr 08 '20 at 00:25
  • Thanks for all the responses. It doesn't sound like there is a definitive option, more code preference. Have a GREAT Day! – Joel Prest Apr 08 '20 at 12:27

1 Answers1

0

I would prefer a combination of both

Dim cCLS As New myClass()
For Each drPerson As DataRow In dtData.Rows
    Dim dsPers = cCLS.Get_PersonByID(drPerson("person_id"))
    ' Code block goes here to do something with dataset object
Next

No unnecessary DataSet constructor calls.

cCLS is a single instance used in all iterations, so it is declared outside the loop

dsPers is a new instance in each iteration of the loop and has no significance outside the context of the loop, so declare it inside.

djv
  • 15,168
  • 7
  • 48
  • 72
  • Not to be _too_ nitpicky, as I prefer this style too, but I don't know if it answers the question as asked. "Is Option A or Option B faster" is answered with "well, Option C is faster/better than both". Even if it's true, it might not get to the nugget of understanding OP wanted, or else we might miss what is different about Options A and B. – Sean Skelly Apr 07 '20 at 20:22