JRS: In article <BE************ ****@news.optus .net.au>, dated Fri, 4
Mar 2005 05:48:17, seen in news:comp.lang. javascript, RobG
<rg***@iinet.ne t.auau> posted :
A little while ago I opined that do/while loops are harder to
read than for loops, and therefore I preferred using for loops.
However, it was pointed out that do/while has significant
performance benefits
...
I did not see you testing anything matching the first loop below, which
seems to me quite readable after one has seen the construction a couple
of times :
N = 1e6
D1 = new Date()
j = N ; while (j--) {}
D2 = new Date()
for (j=0 ; j<N ; j++) {}
D3 = new Date()
for (j=N-1 ; j>=0 ; j--) {}
D4 = new Date()
x = [D2-D1, D3-D2, D4-D3] // gets 2480,4990,3850
Note - two are backwards, one forwards.
In the two FOR loops, j>=0 seems faster than j<N, as is reasonable.
Most loops will be dominated by the body of the loop; but the simplest
WHILE takes half the time of the obvious FOR ... for me.
Then
j = N ; while (j--) {}
for (j=N ; j-- ; ) {}
have similar high speeds - not surprisingly - and the second looks like
a FOR loop.
Perhaps the important point is that the increment and the test should be
combined.
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.