in reply to Re: Shortcutting grep in boolean context
in thread Shortcutting grep in boolean context

As the list gets larger, the overhead of copying the list into @_ increases.
  • Comment on Re^2: Shortcutting grep in boolean context

Replies are listed 'Best First'.
Re^3: Shortcutting grep in boolean context
by LanX (Saint) on Jan 03, 2017 at 22:07 UTC
      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.