June 28, 2016 Re: 4x faster strlen with 4 char sentinel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jay Norwood | 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.
>
> 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;
> }
>
> This is the timing of my test case, which I can post if anyone is interested.
> strlen\Release>strlen
> 2738
> 681
Did you also compare to strlen from libc? I'd guess GNU libc uses a lot more tricks like vector instructions.
|
June 28, 2016 Re: 4x faster strlen with 4 char sentinel | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Tuesday, 28 June 2016 at 01:53:22 UTC, deadalnix wrote:
> 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.
>>
>> 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;
>> }
>>
>> This is the timing of my test case, which I can post if anyone is interested.
>> strlen\Release>strlen
>> 2738
>> 681
>
> If we were in interview, I'd ask you "what does this returns if you pass it an empty string ?"
Since no one is answering:
It depends on the memory right before c. But if there is at least one 0 right before it - which is quite likely - then you get some crazy big number returned.
|
June 28, 2016 Re: 4x faster strlen with 4 char sentinel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sebastiaan Koppe | On Tuesday, 28 June 2016 at 09:31:46 UTC, Sebastiaan Koppe wrote:
>> If we were in interview, I'd ask you "what does this returns if you pass it an empty string ?"
>
> Since no one is answering:
>
> It depends on the memory right before c. But if there is at least one 0 right before it - which is quite likely - then you get some crazy big number returned.
Yes, the test checked for 0 length but not with a preceding 0. I posted the fix.
if (c is null || *c==0)
|
June 28, 2016 Re: 4x faster strlen with 4 char sentinel | ||||
---|---|---|---|---|
| ||||
Posted in reply to qznc | On Tuesday, 28 June 2016 at 09:18:34 UTC, qznc wrote:
> Did you also compare to strlen from libc? I'd guess GNU libc uses a lot more tricks like vector instructions.
I did test with the libc strlen, although the D libraries did not have a strlen for dchar or wchar. I'm currently using this for comparison, and playing around with shorter string lengths:
nothrow pure size_t strlen(const(char)* c) {
if (c is null )
return 0;
const(char)* c_save = c;
while (*c) {
c++;
}
return c - c_save;
}
I'm also trying some tests on a PA device where I have tools to look at cache hits, misses, branches mispredicted. Similar C code.
|
Copyright © 1999-2021 by the D Language Foundation