Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Shortcutting grep in boolean context

by perlancar (Hermit)
on Jan 03, 2017 at 22:18 UTC ( [id://1178885]=note: print w/replies, xml ) Need Help??


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

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.

  • Comment on Re^4: Shortcutting grep in boolean context

Replies are listed 'Best First'.
Re^5: Shortcutting grep in boolean context
by LanX (Saint) on Jan 03, 2017 at 23:27 UTC

      I was not aware of List::MoreUtils::firstval. Okay, added it to the benchmark. It mostly has the same speed as List::Util::first, I wonder what the differences are.

      % bencher -Ilib -m PERLANCAR/grep_bool --include-participant-pattern List::
      +---------------------------+----------+-----------+-----------+------------+---------+---------+
      | participant               | dataset  | rate (/s) | time (μs) | vs_slowest |  errors | samples |
      +---------------------------+----------+-----------+-----------+------------+---------+---------+
      | List::Util::first         | notfound |      3900 |   250     |      1     | 1.8e-06 |      20 |
      | List::MoreUtils::firstval | last     |      3980 |   251     |      1.01  | 5.3e-08 |      20 |
      | List::MoreUtils::firstval | notfound |      4000 |   250     |      1     | 1.1e-06 |      20 |
      | List::Util::first         | last     |      4100 |   240     |      1     | 4.8e-07 |      20 |
      | List::Util::first         | first    |     46400 |    21.6   |     11.8   | 2.1e-08 |      31 |
      | List::MoreUtils::firstval | first    |     46406 |    21.549 |     11.788 | 1.5e-10 |      20 |
      +---------------------------+----------+-----------+-----------+------------+---------+---------+
      
        > I wonder what the differences are.

        I wouldn't be surprised if it's the same code.

        IIRC (?) were these features first introduced in List::MoreUtils and later in List::Util .

        Thanks for the benchmark, have to look closer into it.

        It's very surprising that the XS code may be so inefficient.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1178885]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found