4x faster strlen with 4 char sentinel

chmike via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Sun Jun 26 22:27:12 PDT 2016


On Monday, 27 June 2016 at 05:10:37 UTC, chmike wrote:
> If you store the string size in a four byte field, you get a 
> hard to beat fast strlen. :)

If you have the constrain to work with null terminated strings, 
then it could be interesting to look at SIMD as was suggested or 
use one of the following algorithms to test the presence of a 0 
byte in a 4 byte or 8 byte integer when simd is not available.

https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord

Ending strings with a single null byte/char is to save space. It 
was critical in the 70´s when C was created and memory space was 
very limited. That's not the case anymore and I guess the reason 
why you assume that ending a string with 4 null chars is OK.


More information about the Digitalmars-d-announce mailing list