more useful options | |
PerlMonks |
Re^2: I prefer my indexes to start at:by afoken (Chancellor) |
on Sep 09, 2022 at 19:24 UTC ( [id://11146795]=note: print w/replies, xml ) | Need Help?? |
Wasn't it at some stage, with x86 assembly at least, that a comparison with zero (jump-zero instruction) was faster than comparing with any arbitrary integer? This could have had (aeons ago) an effect in looping through an (edit: zero-index)-array (and preferring the backwards loop) ? It actually depends on the machine implementation. A forward loop from 0 to N-1 may end up with assembler code similar to this pseudo-code:
If the machine allows compare with a constant, one register may be sufficient:
The same loop running backwards from N down to 1 may be more efficient IF the decrement operation sets the zero flag:
On the Z80, there is a special instruction DJNZ (decrement B and jump if non-zero) for exactly this purpose. It does not even affect the flags, it's all handled internally. The 8051 also has a DJNZ that can work on memory or a choosen register. The x86 processors have LOOP/LOOPE/LOOPNE. LOOP is like DJNZ using CX, ECX or RCX, LOOPE and LOOPNE additionally check the zero flag. x86 processors have the REP/REPZ/REPNZ prefixes for a similar purpose. It repeats the follwing string instruction until CX/ECX/RCX is zero. REPZ also aborts if the zero flag is cleared, REPNZ also aborts if the zero flag is set. String operations are INS (read from I/O port to memory), MOVS (copy memory, i.e. memcpy, strcpy), OUTS (write from memory to I/O port), LODS (read from memory to accumulator), STOS (write accumulator to memory, i.e. memset), CMPS (compare memory, i.e. strcmp, memcmp), SCAS (compare memory to accumulator, i.e. strchr, strlen). Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
In Section
Past Polls
|
|