views:

3560

answers:

2

Which of the following has the best performance?

I have seen method two implemented in JavaScript with huge performance gains, however, I was unable to measure any gain in C# and was wondering if the compiler already does method 2 even when written like method 1.

The theory behind method 2 is that the code doesn't have to access DataTable.Rows.Count on every iteration, it can simple access the int c.

Method 1

for (int i = 0; i < DataTable.Rows.Count; i++) {
    // Do Something
}

Method 2

for (int i = 0, c = DataTable.Rows.Count; i < c; i++) {
    // Do Something
}
+2  A: 

No, it can't do that since there is no way to express constant over time for a value.

If the compiler should be able to do that, there would have to be a guarantee from the code returning the value that the value is constant, and for the duration of the loop won't change.

But, in this case, you're free to add new rows to the data table as part of your loop, and thus it's up to you to make that guarantee, in the way you have done it.

So in short, the compiler will not do that optimization if the end-index is anything other than a variable.

In the case of a variable, where the compiler can just look at the loop-code and see that this particular variable is not changed, it might do that and load the value into a register before starting the loop, but any performance gain from this would most likely be negligible, unless your loop body is empty.

Conclusion: If you know, or is willing to accept, that the end loop index is constant for the duration of the loop, place it into a variable.


Edit: Re-read your post, and yes, you might see negligible performance gains for your two cases as well, because the JITter optimizes the code. The JITter might optimize your end-index read into a direct access to the variable inside the data table that contains the row count, and a memory read isn't all that expensive anyway. If, on the other hand, reading that property was a very expensive operation, you'd see a more noticable difference.

Lasse V. Karlsen
Thanks for that excellent response :)
GateKiller
A: 

can the property DataTable.Rows.Count be used in conjunction with if clause like: if (DataTable.Rows.Count == 1) ... ?

Rajalakshmi