2

I have been messing with JavaScript tests, and I did try this code

var count = 0;
while(count < 100000000)
{
    count++;
}

VS

var count = 0;
do
{
    count++;
} while(count < 100000000);

and here are the results:

enter image description here

So I googled it , but did not find any good answer. Can any one explain to me why a while is more efficient than do-while?

Diptox
  • 1,809
  • 10
  • 19
  • 3
    Different optimizations in that implementation? Flawed test? Your email was running in the background? Doesn't really matter. Use the one you need. –  Oct 07 '16 at 13:28
  • Also http://stackoverflow.com/questions/390605/while-vs-do-while and http://stackoverflow.com/questions/3003456/why-use-a-do-while-loop – TylerH Oct 07 '16 at 13:30
  • but as explained here , http://stackoverflow.com/a/20472425/5365770 , the do-while is more efficient than the while , but the results shows another thing – Diptox Oct 07 '16 at 13:35
  • @TylerH: The question should be closed but I don't see how either that Java question or those language-agnostic ones are a duplicate. –  Oct 07 '16 at 13:35
  • @squint various answers explain the speed differences – TylerH Oct 07 '16 at 13:35
  • i did read all pages , still no answer and one of the answers show the invert result – Diptox Oct 07 '16 at 13:35
  • @Diptox: while/do while are syntax. Syntax doesn't have performance, implementations do. There's no single answer. The code you write is just describing something. The implementation takes that and turns it into actual, executable code. –  Oct 07 '16 at 13:37
  • @TylerH: No, they really don't. Aside from the extra iteration (assuming the break condition is identical), they're the same. Now an implementation may have one syntax optimized over the other, but that doesn't have anything to do with the syntax. Some other implementation may have that reversed. Those would just be inconsistencies. –  Oct 07 '16 at 13:39
  • @squint did not get what you say – Diptox Oct 07 '16 at 13:47
  • @squint Sure they do. You just have to read into the answers a bit and understand that OP's premise (that one is inherently faster than the other) is a false one. – TylerH Oct 07 '16 at 13:55
  • @Diptox: Syntax is the characters you type into your code that describe a behavior of your program. That syntax gets read by a parser and gets compiled into actual code that runs. That process is done by people who want to take what you've provided and turn it into the most efficient code that they can. Sometimes the performance optimizations are inconsistent. The `do` and `do-while` loops describe nearly the same behavior. Depending on the compile-time optimizations, they could be rewritten to be identical. Or not. If one implementation produces genuinely faster code today, that... –  Oct 07 '16 at 13:58
  • could change with the next release. Imagine looking at blueprints for two nearly identical homes. Would it make sense to ask which one is more structurally sound? The answer of course would be neither, though one could be more sound depending on how it is constructed. –  Oct 07 '16 at 13:58
  • @TylerH: I don't see that but even if an answer did touch on performance characteristics from some angle, that's not what those questions are about. Doesn't matter. These micro-optimization questions need to be shut down one way or another. –  Oct 07 '16 at 14:04
  • 1
    Smarted optimizer can optimize this to `var count = 100000000;` What You measure is not `while` vs `do-while`, but result of complex heuristics based optimalization algorithms, which is pretty much random at this level. You measure noise. You should not make any conclusions based on two samples of noise. – Antonín Lejsek Oct 07 '16 at 14:35

0 Answers0