views:

328

answers:

3

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; 
  } 
 }
+2  A: 

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.

rockacola
In my world, 0 is always less than 13, and thus the for loop above will always be executed. Besides, isn't a for loop just syntactic sugar for a do-while loop anyway? Am I just missing something in your explanation?
lc
lc, I don't know how any given language implements `for` or `while` or `do`/`while` behind the scenes, but intuitively it seems that a `for` loop is a specialized `while` loop (not `do`/`while`), and at least in the languages I use, if you pass a `for` loop a boundary parameter that evaluates false on first execution, the loop never runs. Eg. `for(i = 10; i < 9; i++) { /* This never runs */ }`. This is the behavior of a `while` loop.
eyelidlessness
+4  A: 

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

Kaleb Brasee
I'm getting a very large improvement actually. I went from .05 iterations per second to .4, and the only code I changed was from a do/while to a for.
Zach
I don't believe for a second that you are only getting .05 iterations per second.
Ed Swangren
This isn't all my code and it's going over an extensive amount of data, so I'm not sure why you wouldn't.
Zach
I don't really understand how you got a .NET implementation to load off the cassette tape on your VIC-20 so you could get 0.4 iterations per second, but thumbs up for hack value.
Nick Bastin
Wow, do you mean .5 iterations to .4? If so then C# deals with do-whiles drastically differently than Java. I wrote a quick test in Java (count to 1 billion) and the results I got were 110ms vs 106ms, and 103ms vs 104ms.
Kaleb Brasee
No .05, and this is just a change to one function inside of a much larger script that goes over a year of data. It's not just this function itself, but it's the only thing I changed between the two. I think a few people are misunderstanding me, and assuming I mean just this loop, which I don't.
Zach
Zach - are you hitting a database or some other factor? I suspect you have some other factor causing your performance difference. You could change your i < 13 to i < 1300000000 and it would still run in under half a second (assuming you had that many rows in your lines array/enumeration).
Jess
If you seriously have code that only gets through half an iteration per *second* than the loop is the least of your concerns.
Ed Swangren
I am hitting a database but the only thing I changed in my code that got .05 and the code that got .4 was this function. I changed the do/while loop to a for loop of the exact same kind.
Zach
If there's really an order of magnitude difference between a for loop and a do-while, then something must be SERIOUSLY whacked out. Everything's an in-memory data structure, so I can't comprehend how that type of performance difference could happen.
Kaleb Brasee
Not my code, I am locked in to proprietary software that's used for trading and they keep it all very locked down. It's full of bugs and a huge expensive piece of shite, see NinjaTrader. I understand that it will go slow, especially for the time period. My only concern is the for loop being so much faster than the do/while and why.
Zach
Zach, if you actually want to test the difference then you need to pull this loop out and execute it with test data outside of the application. Either that, or you need to use a profiling tool to look at the execution speed. Your methodology for comparison is useless.
phoebus
And how repeatable are those results? Where is the code spending most of its time? If you can't answer those than you did not properly profile, you just stuck a timer at the beginning and end.
Ed Swangren
No shit phoebus, I'm not looking to get too scientific with this, I was just wondering which would be faster.
Zach
If you don't want to hear correct answers then go somewhere else. You are worrying about insignificant parts of your code and you don't seem to get it.
Ed Swangren
And since we are tossing around personal insults, that is some of the ugliest code I have ever seen.
Ed Swangren
It's the only thing I changed and therefore the only reason for the difference. So, I don't really get how. Maybe if you guys weren't such a pedantic bunch worrying about insignificant things such as how scientific my methods are, and just answered the question, like the others here so kindly did.
Zach
...But we *were* answering the question. If you are measuring the wrong thing then your measurements are invalid, no?
Ed Swangren
@Zach That's kind of the point...if your methods are not "scientific" enough, then they really don't tell you anything. Not exactly an "insigificant" factor.
phoebus
I'll admit you guys are probably right, I got very heated and defensive. My apologies, thanks for taking the time to answer my question and offer insight.
Zach
Zach - it's also possible that you changed the definition of the for loop when converting to a while ... seriously, once compiled, the for loop is not different than a while statement
Jess
+3  A: 

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

int i = 0;
while (i < 13)
{
   //all other stuff
   i++;
};
Jess
+1: In general, I'd expect the compiler or interpreter to convert the `for` sugar to `while`.
eyelidlessness