It's just a page to test directly differents loops and respective times with blue "winners" too :)
If you have a really old (or slow) PC please don't visit the page.
If you visit the page and everything stops, close the page and wait few seconds.
Loops Benchmark: http://www.devpro.it/examples/loopsbench/
My JavaScript book is out!
Don't miss the opportunity to upgrade your beginner or average dev skills.
18 comments:
Have you tried the duff fast device?
(just for the sake of it?)
I show you my fastest implementation, have a look at it, please.
http://mykenta.blogspot.com/2006/08/kentaromiura-fastest-device-duffs.html
done ... but your blog version is buggy, last while cannot be --i without an if(i) before -- operator ... I've switched to
while(i--) ... for last loop
P.S. per ora non c'e' nulla di piu' veloce di questo metodo ;)
it seems fast, but neither portable nor always the fastest :P
I've upload this test to show that a loop ... is a loop, then do that in your favourite way and forget while or for performances, these are often the same with every loop ;)
please Andrew, show me an example of when it fails(you said that it's buggy) but I don't see nothing wrong in it ,
but I'm just tired so it could be...
you said that it's buggy) but I don't see nothing wrong in it
I's obvious, I've corrected the infinite loop using i-- instead of --i.
It's really simple
while(--i) when i is zero doesn't break the loop (if(-1)alert("true");)
mmm.. when testVal is 0? when iterations is a multiple of 8..
Yes, I forgot a if(n) just before the last while in my post, thanks for have it pointed out.
I apologize.
I used if(n) too then I thought that a maximum of seven i-- is not a problem for performances ... then I've just modified into while(i--) last piece of code ;)
if you look closest you see that putting if(n) isn't enought, because you lost a cycle.
so if you put if(n) you must put a testVal++ before the while(and lost that millisecond gained)
so testVal-- is ok.
I must say that I do a lot of benchmark with all this methods, and I obtained different results from yours, maybe because I haven't 3d that goes around, maybe because I don't waste memory, or just because I use a function to mesure time and not a object.
if I have time I'd like to public mine results.
try the last test of
http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html
or this
http://www.websiteoptimization.com/speed/10/10-3.html
I must say that I do a lot of benchmark with all this methods, and I obtained different results from yours
If you look at my bench page, every 3 seconds the benchmark is updated.
Memory is free for each bench (I hope garbage collector does a good work).
A function isn't usefull for this test because you can't apply internal operations without an "eval style" ... you need to view real applications, where your "fast duff" is absolutely a weight killer and it's not portable.
Who will copy and paste 10 lines of code to do a simple while ?
Finally I think that we don't need to care about loops optimizzation because in some case, with some script, in some point of execution, a while, as a for or your "fast duff", should be faster or slower, but generally, will be inconsistent if the rest of the script isn't optimized :)
the original "in scope" loop is
like for(var c=0,i;i=arr[c];c++) - maybe this works with "null" variables too (another test maybe, it was just a guess if it checks for (i=arr[c]) == null)) - and just for the record: i found this somewhere in the net, so i did not invent this looping :)
Well, I think these benchmark are enought to view that "everyone", sometime, is the faster.
There are 3 new benchmarks, one from kenta, one from J. Reichardt (not the original version he gave me **) and my final version, that's just an obtrusive way compare other loops.
** Johannes Reichardt showed me a "dedicated" version of a for loop, however since this is a generic performances test, everyone should "solve" the "problem" in a different way (for example using common[index] = {object} instead of push) but with "everyday scripts" we solve problems in "our best ways" but sometimes we would like to know wich loop stye is faster than others.
Finally, as I've just said, I think that every for or while loop, over arrays, is "the best solution", if chosed one is short and fast enought to solve our scripting problems :)
for(var c=0,i;i=arr[c];c++)
if(null) is "always false" ...
if((a = null)) returns a value, then is false again ...
if((a = b) != null) is false another time ... when b has a null value.
Then if you want to "copy" a list of values, your original for loop will not copy every value and will break at first b null value :)
(I forgot) ... that's why I've modified your for loop but if you want I'll remove your name from that page :)
I don´t really get this "null" thing :) - but the real nice thing about the "in scope" way is the fact, that i contains the plain value - so in longer loops it might save some more time since you don´t have to look up every and each variable like arr[c]...
from websiteoptimization
JavaScript for and while loop benchmarks
Shows times for various loop optimizations by Andrea Giammarchi. See also this post JavaScript benchmark for while and for loop.
Johannes, the benchmark pages shows a generic list of loops over same operations, in this case a push Array method but in "every day" cases every other operation.
The goal of a generic loop is to do something in a finite way while the goal of this test page isn't to show the best way to assign a value into an array, then there's anything to change in mine, kenta or yours loop :)
@ Johannes
just for fun, another bench for you :)
array push VS direct assignment
P.S. using "" instead of '' is exactly the same thing for this post benchmark code and its goal ... however sometimes we use '' because we write html tags in this way: attr="value" .
FireFox does ' to " convertion automatically (try this code: alert(function(){return 'hello'})).
Then maybe using " and then \" should be "faster" using firefox or other browsers.
can i use the link as an educational reference?
of course you can :)
Post a Comment