Pardon the use of Bencher:
% bencher -Ilib -m PERLANCAR/grep_bool 
+------------------------+----------+------------+-------------+------------+---------+---------+
| participant            | dataset  | rate (/s)  |   time (ms) | vs_slowest |  errors | samples |
+------------------------+----------+------------+-------------+------------+---------+---------+
| Array::AllUtils::first | notfound |     556    | 1.8         |    1       | 1.3e-06 |      21 |
| Array::AllUtils::first | last     |     559    | 1.79        |    1.01    | 2.7e-07 |      20 |
| grep+die               | last     |     672    | 1.49        |    1.21    | 4.3e-07 |      20 |
| grep+die               | notfound |     677    | 1.48        |    1.22    | 4.8e-07 |      20 |
| foreach+last+do        | last     |    3124.33 | 0.320068    |    5.62082 |   0     |      21 |
| foreach+last+do        | notfound |    3130    | 0.32        |    5.63    | 5.3e-08 |      20 |
| grep                   | first    |    3330    | 0.301       |    5.98    | 5.3e-08 |      20 |
| grep                   | notfound |    3572.41 | 0.279923    |    6.42693 |   0     |      21 |
| grep                   | last     |    3580    | 0.28        |    6.44    | 5.3e-08 |      20 |
| List::Util::first      | last     |    4237.74 | 0.235975    |    7.62389 |   0     |      20 |
| List::Util::first      | notfound |    4237.74 | 0.235975    |    7.62389 |   0     |      20 |
| List::Util::first      | first    |   51781.5  | 0.0193119   |   93.1573  |   0     |      20 |
| grep+die               | first    |  130000    | 0.0079      |  230       | 1.3e-08 |      20 |
| Array::AllUtils::first | first    | 2170000    | 0.000461    | 3900       |   2e-10 |      21 |
| foreach+last+do        | first    | 5478270    | 0.000182539 | 9855.67    |   0     |      20 |
+------------------------+----------+------------+-------------+------------+---------+---------+

This basically benchmarks over an array containing 10,000 items with three cases: first is when the item to be found is at the first element, last is when the item to be found is at the last element, and notfound is when the item searched does not exist in the array (which should be the same performance-wise as last, but added for testing the result).

You can see that List::Util::first is much slower than Array::AllUtils::first or foreach+last+do or grep+die for the 'first' case.

The benchmark scenario is here.


In reply to Re^4: Shortcutting grep in boolean context by perlancar
in thread Shortcutting grep in boolean context by perlancar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.