4x faster strlen with 4 char sentinel

David Nadlinger via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Sun Jun 26 09:59:54 PDT 2016


Hi Jay,

On Sunday, 26 June 2016 at 16:40:08 UTC, Jay Norwood wrote:
> After watching Andre's sentinel thing, I'm playing with strlen 
> on char strings with 4 terminating 0s instead of a single one.  
> Seems to work and is 4x faster compared to the runtime version.

Please keep general discussions like this off the announce list, 
which would e.g. be suitable for announcing a fleshed out 
collection of high-performance string handling routines.

> nothrow pure size_t strlen2(const(char)* c) {
>  if (c is null)
>    return 0;
>  size_t l=0;
>  while (*c){ c+=4; l+=4;}
>  while (*c==0){ c--; l--;}
>  return l+1;
> }

A couple of quick hints:
  - This is not a correct implementation of strlen, as it already 
assumes that the array is terminated by four zero bytes. That 
iterating memory with a stride of 4 instead of 1 will be faster 
is a self-evident truth.
  - You should be benchmarking against a "proper" SIMD-optimised 
strlen implementation.

  — David


More information about the Digitalmars-d-announce mailing list