Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Shortcutting grep in boolean context

by LanX (Saint)
on Jan 03, 2017 at 22:07 UTC ( [id://1178883]=note: print w/replies, xml ) Need Help??


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

You say a short cutting grep has less overhead then List::Util::first() ?

That's strange, can you show a benchmark?

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

Replies are listed 'Best First'.
Re^4: Shortcutting grep in boolean context
by perlancar (Hermit) on Jan 03, 2017 at 22:18 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.

        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 |
        +---------------------------+----------+-----------+-----------+------------+---------+---------+
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found