4x faster strlen with 4 char sentinel

Jay Norwood via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jun 27 20:11:26 PDT 2016


On Tuesday, 28 June 2016 at 01:53:22 UTC, deadalnix wrote:
> If we were in interview, I'd ask you "what does this returns if 
> you pass it an empty string ?"

I'd say use this one instead, to avoid negative size_t. It is 
also a little faster for the same measurement.

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

2738
540
2744



More information about the Digitalmars-d-announce mailing list