4x faster strlen with 4 char sentinel

deadalnix via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Tue Jun 28 00:05:30 PDT 2016


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 ?"
>
> 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

If we were in an interview, I would insist.



More information about the Digitalmars-d-announce mailing list