Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • They are all damn fast, and using any reads much better.

  • Your reasoning is flawed. You are assuming that there is one positive match in the set, and that the set is small. On such a small set you're right any isn't any faster, but superior algorithms rarely show up on small datasets.

    If you would test this with a larger dataset, any would come out much better. On my computer, using the list 1..1000 and a needle of 500, it is in fact faster. The advantage only gets bigger when the haystack gets bigger.

    You're right it's not always better, but it is when it matters t

    • I agree. With such a small set no method is going to take an irritatingly long time. Since you have the program handy could you give it a quick tweak to use a bigger array and re-post the results?

      I also agree with autarch that any reads better, as a way of signalling programmer intent. I'd probably use the any from Perl6::Junction rather than List::MoreUtils; any chance you could add that to your testing as well? Thanks!

  • Can you test how good would perform List::Util::first()?

    • Well, you won't like it... Using the code:

      first => sub { my $x = defined first { $_ eq $search } @letters },

      first is always close to 20% slower than any...

      Search for Z
                Rate    Time first  any
      first  38653/s 25.87us    -- -21%
      any    48955/s 20.43us   27%   --

      Search for M
                Rate    Time first  any
      first  49484/s 20.21us    -- -22%
      any    636

      • Your observation seems to be only true for small arrays. In this plot [radzeit.de] you can see how first() gets better with larger arrays.

        In this benchmark [sourceforge.net] grep and first were searching for the middle element in the array.