-7

Simple question: What is best, method 1 or method 2?

Is method 2 faster/better:

List<int> data = Enumerable.Range(0, 10000000).ToList();
int j = 0;

// Method 1
while (j < data.Count)
{
    // do something
    j++;
}

j = 0;

// Method 2   
while (j++ < data.Count)
{
    // do something
}

or would a simple for-loop still be better?

Update: according to the comments, a subquestion: Should while-loops be avoided or should for-loops be avoided and replaced with these while-loops?

juFo
  • 17,849
  • 10
  • 105
  • 142
  • I do have a feeling, that after jitting & optimization theese two methods will be compiled into the same machine code – undefined May 14 '12 at 09:17
  • 2
    Isn't there slight logical difference? –  May 14 '12 at 09:17
  • There IS a difference, but only that `j` in method 2 will be always 1 ahead of `j` in method 2, considering the amount of iterations, when inside the while body. – SimpleVar May 14 '12 at 09:18
  • @Peter the only *functional* difference is if `j` is used inside the loop. The other differences; really minimal./ – Marc Gravell May 14 '12 at 09:18
  • 3
    Frankly, this is actually a `for` loop or `foreach` loop, and should be written as such, or even better: a `foreach` loop over `Enumerable.Range(...)` ***without*** calling `ToList()` – Marc Gravell May 14 '12 at 09:19
  • updated the question. so should for-loops be used or just method 1? – juFo May 14 '12 at 09:36
  • Strange that this has downvotes but I still get different answers from people. – juFo May 14 '12 at 09:46
  • 1
    If you're worried about performance, use a for loop over an array. The JIT compiler has special range-check-elimination optimizations for that. It will matter more than little things such as where the increment goes. – harold May 14 '12 at 10:08

3 Answers3

2

I would suggest neither, rather I would suggest this

List<int> data = Enumerable.Range(0, 10000000).ToList();
int j = -1;



// Method 1   

while (++j < data.Count)
{
    // do something
}

int j = 0;
do
    {
        //anything
    } while (++j<data.count);

pre-increment operation is faster than post, although a small performance advantage

zahreelay
  • 1,742
  • 12
  • 18
  • Is the "++i is faster than i++" argument still valid? I thought I read somewhere that it wasn't anymore (or I could be wrong???)? – intrigued_66 May 14 '12 at 11:12
  • @user1107474 I would love to see where you found that. – juFo May 14 '12 at 11:40
  • @juFo I just did a quick search on stackoverflow and the answer could be language-dependent. I may have seen a post referring to a different language. – intrigued_66 May 14 '12 at 12:42
1

There is no difference (at least significative in terms of performance, you should measure on really heavy calculations)

I would choose the first one, cause it seems clearer, to me.

Tigran
  • 61,654
  • 8
  • 86
  • 123
-1

Both are take same execution time,There is no difference.You can use any of that in your poject.

Shin
  • 280
  • 1
  • 2
  • 11