4x faster strlen with 4 char sentinel

Ola Fosheim Grøstad via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jun 27 09:38:58 PDT 2016


On Monday, 27 June 2016 at 16:22:56 UTC, Jay Norwood wrote:
> This strlen2 doesn't require special alignment or casting of 
> char pointer types to some larger type. That keeps the strlen2 
> implementation fairly simple.

Yes, and the idea of speeding up strings by padding out with 
zeros is not new. ;-) I recall suggesting it back in 1999 when 
discussing the benefits of having a big endian cpu when sorting 
strings. If it is big endian you can compare ascii as 32/64 bit 
integers, so if you align the string and pad out with zeros then 
you can speed up strcmp() by a significant factor. Oh, here it is:

http://disinterest.org/resource/MUD-Dev/1999q1/009759.html

Of course, this is all moot now, little endian + simd has made 
such tricks redundant. Simd probably makes your strlen2 redundant 
too. The bottle neck tends to be memory access/prefetching for 
simple algorithms.



More information about the Digitalmars-d-announce mailing list