3 ms·
I wonder if there are any architectures where the naive strlen performs better.
by icodestuff 12y ago
I wonder if there are any architectures where the naive strlen performs better.
- csense 12y agoThe Zilog Z80 would definitely be in this category. It would be really complicated for a compiler or human programmer to work around the quirks of the Z80 instruction set, where only certain registers can be used for certain operations. Even an optimal implementation of the word solution would probably be quite a bit slower than a straightforward naive implementation. The Z80 instruction set only supports word reads from constant addresses (but bytes can be read from an address specified in any register pair). The Z80 has very few registers, so the more complicated algorithm will probably face register pressure. Also, word comparison is only implemented for the HL, DE register pair unless you want to use an index register which requires an instruction prefix, which will make your code even slower. But memory reads from an address in a register pair other than HL or an index register can only go to the accumulator register A, thus you'd need HL for both the comparison and the address. So you need more instructions to save and restore HL. (You can't even use the fastest option EX DE,HL because DE is also needed for the comparison.)