Tutorial :C# which is faster do/while or for?



Question:

I have a C# .NET 2.0 script and I want to know why the following code would be faster than a do while loop of the same kind.

private double getStop(double avgPrice, bool longTrading)      {          double stopS = 0.0;          double stopL = 0.0;            for (int i = 0; i < 13; i++)          {              if (i == 0 || i == 12)              {                  stopS = 0.0;                  stopL = 0.0;              }              else              {                  if ((lines[i] - ((lines[i] - lines[i - 1]) / 2)) < avgPrice && avgPrice < (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2)))                  {                      if (avgPrice < lines[i])                      {                          stopL = (lines[i] - ((lines[i] - lines[i - 1]) / 2));                          stopS = lines[i];                      } else {                          stopL = lines[i];                          stopS = (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2));                      }                  }              }          }            if (longTrading)          {              return stopL;             } else {              return stopS;             }         }  

Also, would it be faster just to explicitly state each if statement instead of doing them inside of a for loop?

Being that this was answered so fast, why would this run far slower than the above code?

private double getStop(double avgPrice, bool longTrading)      {          double stopS = 0.0;          double stopL = 0.0;            for (int i = 0; i < 13; i++)          {              if (i == 0 || i == 12)              {                  stopS = 0.0;                  stopL = 0.0;                  skip = true;              }                if (!skip && (lines[i] - ((lines[i] - lines[i - 1]) / 2)) < avgPrice && avgPrice < (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2)))                  {                      if (avgPrice < lines[i])                      {                          stopL = (lines[i] - ((lines[i] - lines[i - 1]) / 2));                          stopS = lines[i];                      } else {                          stopL = lines[i];                          stopS = (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2));                      }                  }              }             skip = false;          }            if (longTrading)          {              return stopL;             } else {              return stopS;             }         }  


Solution:1

They should be essentially equivalent. Your 'for' loop gets evaluated as:

int i = 0;  while (i < 13)  {     //all other stuff     i++;  };   


Solution:2

The performance difference should be negligible, but the for loop is clearer so I would go with that.


Solution:3

loop variations aren't too much different, it depends on the context and the programming language.

But my opinion is, for statement don't do until the constaint(s) is/are matched, therefore it should be faster than do/while.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »