0

I am loading a huge dataset into memory. The code basically loops through a DataTable and copies values from a DataRow into int variables. For some reason, the variables that I declare inside the loop process almost twice as fast than those that are declared at the top of the method.

Below is the screenshot from Ants Perf Profiler. As you can, line siteID = (int) oDR[FIELD_SITE_ID] is almost twice as slow as the previous line. Why would that be?

enter image description here

AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • 2
    have you tried it extracting the same field from the dataset? It could be something tied with the underlying data type and the specific casting for `oDR[FIELD_SITE_ID]` – DiskJunky Feb 19 '13 at 22:26
  • Have you tried instantiating `siteID` within the same loop? – PiousVenom Feb 19 '13 at 22:27
  • Perhaps looking up `FIELD_SITE_ID` on the row just takes longer than `FIELD_BREAK_ID`. – Magnus Feb 19 '13 at 22:29
  • 1
    Try reorder assignments and tell about your observation. – Hamlet Hakobyan Feb 19 '13 at 22:30
  • There should be no difference, related: http://stackoverflow.com/questions/8535846/is-it-better-to-declare-a-variable-inside-or-outside-a-loop – Tim Schmelter Feb 19 '13 at 22:34
  • 1
    You jump to conclusions: You assume that the variable has to do with it. There is no evidence pointing to that. – usr Feb 19 '13 at 22:37
  • @DiskJunky There is no specific casting - these are all ints in both database and c# code. – AngryHacker Feb 19 '13 at 22:55
  • @TyrionLannister I can't instantiate siteID in the loop since it's needed down below after the loop. – AngryHacker Feb 19 '13 at 22:56
  • @Magnus That's a possibility - I'll try switching to numeric indexer and see what happens. But then the difference between FIELD_INVENTORY_POD_ID and FIELD_BREAK_ID should be more pronounced. – AngryHacker Feb 19 '13 at 22:58
  • 1
    Could you post the code of the whole method? Is it possible that `siteID` is closed-over in a lambda (which would mean it's not actually a local variable, but a field in an autogenerated class), or something like that? – svick Feb 20 '13 at 00:26

1 Answers1

0

Couldn't it be that oDR[FIELD_SITE_ID] is what's slow here? Try assigning it to a variable in the loop and then assign that to the siteID variable. If I'm correct you should see that the penalty has moved from your sideID assignment to the statement getting the value from oDR.

int siteIDtmp = (int)oDR[FIELD_SITE_ID];
siteID = siteIDtmp;
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157