faster splitter
Chris via Digitalmars-d
digitalmars-d at puremagic.com
Fri May 27 07:41:29 PDT 2016
On Friday, 27 May 2016 at 14:06:09 UTC, Chris wrote:
>
> This outperforms both `manual_find` and the improved `std find`
>
> string findStringS_Manual(string haystack, string needle)
> {
>
> if (needle.length > haystack.length)
> return haystack[$..$];
> outer:
> for (auto i = 0; i < haystack.length; i++)
> {
> if (haystack[i] != needle[0])
> continue;
> for (size_t j = i+1, k = 1; k < needle.length; ++j, ++k)
> if (haystack[j] != needle[k])
> continue outer;
> return haystack[i..$];
> }
> return haystack[$..$];
> }
>
Little bug fix:
for (size_t j = (i+1 < haystack.length) ? i+1 : i, k = 1; k <
needle.length; ++j, ++k)
Else it will be out or range when you look for "za" and the
string ends with "z". There is a slight performance penalty, but
it's still faster than the rest. Maybe there are cleverer ways.
But every little check and decision comes with a performance
penalty.
The improved `std find` comes very close to the manual function
above now, sometimes it's even faster or at least as fast.
std find took 12573666
manual find took 7418454
my std find took 6903854 <===
findStringS took 7166720
findStringS_ took 6579529 <===
std find took 11849892
manual find took 7407091
my std find took 6573102 <===
findStringS took 7296610
findStringS_ took 6573214 <===
std find took 10744361
manual find took 7576133
my std find took 6332014 <===
findStringS took 7224946
findStringS_ took 6555976 <===
More information about the Digitalmars-d
mailing list