PDA

View Full Version : for i



jscheuer1
12-01-2007, 09:06 AM
A little ways back we got into discussing what the most efficient way to iterate using 'for i' was, and it appeared to me that we settled on something like so:


for (var i = some.length-1; i > -1; --i)

or:


for (var i = some.length-1; i >= 0; --i)

I just realized that this should mean the same thing:


for (var i = some.length-1; i+1; --i)

and it worked!

djr33
12-01-2007, 10:07 AM
for (var i = some.length; i; --i)

tech_support
12-01-2007, 10:13 AM
What's wrong with for (var i=0; i < some.length; i++)?

Oh, and for (var i = some.length-1; i+1; --i) will skip the last value.

ddadmin
12-01-2007, 11:22 AM
How much savings are we really talking here though, at the expense of legibility IMO? As long as you remember to cache any objects in the conditional part of the loop, I think that should suffice in most cases right?

djr33
12-01-2007, 12:14 PM
Well, I've wished for a long time that there was a better loop. "For" is misleading, and, I'd say, only useful in limited circumstances. It's nice to have a default variable for counting repetitions, but when this isn't needed, the bulk of the statement seems to be excessive. I also like for loops for some other situations, that might not be readily apparent. Replacing many while loops with for loops can be nice, such as (though this is PHP) for ($i=0; $row=mysql_fetch_array($result);$i++) { echo 'Entry '.$i.': '.$row[0]; }, and other situations.
However, there should just be a basic loop-- repeat(5) { }, which is what I'd think "For" would mean... for(5) {...}

Twey
12-01-2007, 12:47 PM
Python has no for loop like this, just a for/in loop.
Oh, and for (var i = some.length-1; i+1; --i) will skip the last value.No it won't. It's less efficient than a normal comparison though, since i + 1 here is more-or-less equivalent to i + 1 != 0, an addition and comparison rather than just a comparison.
for (var i = some.length; i; --i)for(var i = some.length; i--; ) if you want to abuse the for structure :) Due to the wacky values of i during execution, though, most of these are usually impractical.
How much savings are we really talking here thoughNot much really.
at the expense of legibility IMOThe for loop as a whole is very difficult to read. The only reason we can understand "normal" for loops so easily is because we've come to recognise the idiom, and the reason these loops are hard to read is because they don't fit it so we have to actually read them.
there should just be a basic loop-- repeat(5) { }You can implement it as a function:
function do_times(numTimes, func) {
while(numTimes--)
func();
}Bit of extra function cruft:
do_times(5, function() {
do_something();
});

djr33
12-01-2007, 01:08 PM
And somehow making a function, making an inner function and calling that is easier than just a for loop? I didn't say they were hard.... just that it would be nice to have a basic looping method based on numerical values, not just conditions. repeat(5), rather than any conditions, counting, etc.

I think you meant do_times(5, 'function') {. (in addition to not closing the parentheses, you used a function call rather than the name as the string to later be executed in the do_ function. Or, maybe not. I think I missed something.

Twey
12-01-2007, 01:16 PM
I didn't say they were hard.... just that it would be nice to have a basic looping method based on numerical values, not just conditions. repeat(5), rather than any conditions, counting, etc.Which is exactly what I just wrote :)
I think you meant do_times(5, 'function') {. (in addition to not closing the parentheses, you used a function call rather than the name as the string to later be executed in the do_ function. Or, maybe not. I think I missed something.No I didn't, and yes you did, although I'm not entirely sure what. Executing strings is bad form.

jscheuer1
12-01-2007, 02:53 PM
The efficiency of using the prefixed decremented loop instead of the appended incremented loop is only significant when there are a large number of items to loop through and/or a large number of loopings.

However, when writing scripts, it is often unknown how many items there will be, and sometimes the number of executions of looping constructs is also unknown.

Using, as put forth by someone else in this thread:


for (var i = some.length; i; --i)

For the usual sort of array looping done (checking each item in the array), with that the variable i will never be able to equal 0, which will leave off the first element in 'some', and check a non-existent element:

some[some.length]

Twey
12-01-2007, 03:51 PM
The efficiency of using the prefixed decremented loop instead of the appended incremented loop is only significant when there are a large number of items to loop through and/or a large number of loopings.E.G. around 10,000 on modern CPUs :)

Trinithis
12-01-2007, 05:33 PM
in addition to not closing the parentheses, you used a function call rather than the name as the string to later be executed in the do_ function. Or, maybe not. I think I missed something.

He's using an anonymous function (or function literal or function expression . . . etc; take your pick) to do the job. Once parsed, it returns a reference to itself, which the code uses.


do_times(5, function() {
do_something();
});

I've read some articles a few months ago about loop efficiency, and they said do-while loops are efficient using a construct akin to

do{...}while(--i);
But I recall testing it and it happened to be slower.

jscheuer1
12-01-2007, 11:21 PM
I've read some articles a few months ago about loop efficiency, and they said do-while loops are efficient using a construct akin to

In the tests and discussions I was referring to earlier, while was the biggest loser.

But, if you aren't dealing with many items, and if the while method doesn't introduce extraneous items (as it can in certain cases), the statement itself may parse faster, and the execution rate on a small sample be such an insignificant factor, that it would be efficient.