c# - Is one of these for loops faster than the other? -
for (var keyvalue = 0; keyvalue < dwhsessiondto.keyvalues.count; keyvalue++) {...} var count = dwhsessiondto.keyvalues.count; (var keyvalue = 0; keyvalue < count; keyvalue++) {...} i know there's difference between two, 1 of them faster other? think second faster.
yes, first version much slower. after all, i'm assuming you're dealing types this:
public class slowcountprovider { public int count { { thread.sleep(1000); return 10; } } } public class keyvalueswithslowcountprovider { public slowcountprovider keyvalues { { return new slowcountprovider(); } } } here, first loop take ~10 seconds, whereas second loop take ~1 second.
of course, might argue assumption you're using code unjustified - point right answer depend on types involved, , question doesn't state types are.
now if you're actually dealing type accessing keyvalues , count cheap (which quite likely) wouldn't expect there difference. mind you, i'd prefer use foreach possible:
foreach (var pair in dwhsessiondto.keyvalues) { // use pair here } that way never need count. then, haven't said you're trying inside loop either. (hint: more useful answers, provide more information.)
Comments
Post a Comment