4x faster strlen with 4 char sentinel

Bauss via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jun 27 21:28:35 PDT 2016


On Tuesday, 28 June 2016 at 03:58:23 UTC, Jay Norwood wrote:
> On Tuesday, 28 June 2016 at 03:11:26 UTC, Jay Norwood wrote:
>> 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 ?"
>
> oops.  I see ... need to test for empty string.
>
> nothrow pure size_t strlen2(const(char)* c) {
>   if (c is null || *c==0)
>      return 0;
>   const(char)* c_save = c;
>   while (*c){ c+=4; }
>   while (*c==0){ c--; }
>   c++;
>   return c - c_save;
> }

Why not just do

nothrow pure size_t strlen2(const(char)* c) {
   if (!c)
     return 0;

   ...
}


More information about the Digitalmars-d-announce mailing list