in reply to Benchmarking "Are all these characters in this sentence?"

Re: chop, the "buk" solution fails on this test:
[ "abxcd zwe rrv", "0", 0 ],
because 0 is false.

On a more conceptual level, it fails in that the return value of chop on an empty string is not documented. But in practice, this would be the fix:

1+index $s, $c or return 0 while length($c = chop $w);

Replies are listed 'Best First'.
Re^2: Benchmarking "Are all these characters in this sentence?"
by RMGir (Prior) on Sep 01, 2008 at 11:23 UTC
    Good point!

    I added that test case, as well as a

    [ "0", "0", 1],
    to test for those pesky perl falsehood values.

    buk and buk2 both pass this new test and fail yours. (Edit: Corrected, thanks ysth)

    Adding length makes buk2 correct, and doesn't penalize the speed much:

    buk2_len                    4610/s                 4007%
    buk2                        4791/s                 4168%  
    

    Mike

      Try it this way. I believe it addresses both the real and conceptual errors and runs even faster:

      buk3 => sub { 1+index $_[0], chop $_[ 1 ] or return for 1 .. length $_[ 1 ]; 1; },

      Who says optimisation can't also result in correct code. Such fun :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        You're right - that passes all tests and wins by a good margin.

        You're quite good at this game :)

        I'll update the parent post.


        Mike
        Corrected, thanks!

        Mike