|
|
|
|
haystack, needle, (result - haystack) or -1 for NULL { "", "", 0 }, { "", "x", -1 }, { "x", "", 0 }, // your function returned offset = 1 { "x", "x", 0 }, { "xy", "x", 0 }, { "xy", "y", 1 }, { "xyx", "x", 2 }, { "xyx", "y", 1 }, { "xyx", "z", -1 }, { "xyx", "", 0 }, // your function returned offset = 3 |
doesn’t pass the litmus tests |
|
|
|
|
|
|
averages: #1 0.812 us last_strstr #2 0.825 us strrstr #3 1.404 us theo #4 1.417 us cordelia #5 2.176 us backwards_memcmp #6 2.581 us seeplus #7 2.946 us digitalross #8 723.103 us sinan |
strstr()
was already considered (it is called “last_strstr()
” in the test suite). It regularly rates first or second in the tests.averages: #1 0.770 us strrstr #2 0.813 us last_strstr #3 1.481 us theo #4 1.539 us cordelia #5 2.116 us seeplus #6 2.121 us backwards_memcmp #7 2.674 us stacked #8 2.945 us digitalross #9 714.874 us sinan |
Sadly, your algorithm rates poorly. |
|
|
averages: #1 0.766 us strrstr #2 0.817 us last_strstr #3 1.520 us theo #4 1.554 us cordelia #5 2.087 us seeplus #6 2.111 us backwards_memcmp #7 2.678 us stacked #8 2.942 us digitalross #9 717.930 us sinan |
Alas, no significant improvement |
the entire test program is there in my posting on SO |
|
|
|
|
|
|
it could be faster to do 64 bit int comparisons (8 times faster!) |
|
|