My original question to Jason was clarification on *why* (--i -(-1)) would run faster than (i--) as it seemed to me that two calculations per loop would run slower than 1. Since he used an expression I've never seen before I figured he might might know the reasoning behind the optimization. It does in fact surprise me that this method is faster.

You mentioned the reason it ran faster was because it compiled down to less lines of code. But we see this isn't the case. I need to find good documentation/reference on p-code to better understand what is going on under the hood. So far my google searches turn up nothing good so any additional links would be appreciated.

My tests so far...

By itself, (i--) runs faster than (--i -(-1)). However, when used as the condition for a while loop (i--) is slower. That's the part I don't get. Why is it slower? What's different about being the condition of a loop?

Here's my two tests. I alternated comments so that each of the four tests ran by themselves:

//--------------------------------------------------------------
// (i--) vs. (--i -(-1))
// Test 1: (i--) is 448 millis faster as standalone
// Test 2: (i--) is 140 millis slower as loop conditional
//
var i,begin,end;
i = 1000000;
begin = getTimer();
//---------------------------
// Test 1 A
while (i) { (i--); }
end = getTimer();
trace("1A:" + (end-begin));    // avg 1676
//---------------------------
// Test 1 B
//while (i) { (--i -(-1)); }
//end = getTimer();
//trace("1B:" + (end-begin));  // avg 2124
//---------------------------
// Test 2 A
//while (i--) {}
//end = getTimer();
//trace("2A:" + (end-begin));  // avg 1620
//---------------------------
// Test 2 B
//while (--i -(-1)) {}
//end = getTimer();
//trace("2B:" + (end-begin));  // avg 1480

Any thoughts or comments appreciated. Claus, Darron, you guys reading?... I'd bet you guys could shed some light on p-code.

Yes, I definately agree with you that people should learn to write different ways so that it's all readable code. I'm sure with time (--i -(-i)) could look pretty natural to me though it looks a bit odd at the moment. I'm always looking to learn something new and not afraid to ask questions when I see something I don't understand.


James O'Reilly
www.jamesor.com



Steven Sacks | BLITZ wrote:
There has been extensive testing on this (search the archives) and it's
been proven to my satisfaction that pre-decrementated loops are
consistently faster than post-decremented loops, and specifically that
while (--i -(-1)) is faster than while (i--), less p-code or not.

To the point that it's more readable, that's like saying that you prefer
quarter notes instead of sixteenth notes when reading music.  Once you
write while (--i -(-1)) a bunch, you have no trouble reading it.  It's a
hell of a lot easier to read than the fastest for loop syntax.

for (var i = myArray.length; --i -(-1); ) {}

I mean, if you find inline conditionals or non-braced if statements hard
to read, does that mean you shouldn't write them or that you should
learn to write them so you can learn to read them.

I didn't like mustard when I was younger.  Does that mean I shouldn't
ever like mustard?   ;)

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to