开发者

Which is best for performance

Can anyone shed some light on this for me. Which would be better for performance

    foreach (DataRow row in prodStockLevels.Rows)
    {
     开发者_开发技巧  string stock = row["stpt_Stock"].ToString();
       textbox.text = stock;
    }

or

    foreach (DataRow row in prodStockLevels.Rows)
    {
       textbox.text = row["stpt_Stock"].ToString();
    }

Would there be much difference even if the datatable is huge. Just want to know is it better for performance if the data is passed directly.


There'll be no discernable difference in efficiency - I doubt that it's even measurable, if it exists at all.

The fact that you're assigning to a local variable will basically be irrelevant. The value of the expression row["stpt_Stock"].ToString() will end up on the stack either way. The only difference is whether that stack slot has a name or not.

In the debugger it might make a tiny but just-about measurable difference as it may have some additional implications - but certainly not enough to worry about.

I think I'd be more worried about the fact that you're repeatedly assigning values to the same textBox.Text property - only the last one is actually going to be relevant. You'd be better off just fetching the last row...


No difference. Probably the exact same code after compilation.


No difference.
The following will be faster than both:

 textbox.text = prodStockLevels.Rows.Last()["stpt_Stock"].ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜