Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Shortcutting grep in boolean context

by Anonymous Monk
on Jan 03, 2017 at 20:50 UTC ( [id://1178872]=note: print w/replies, xml ) Need Help??


in reply to Shortcutting grep in boolean context

would it break many programs?

Not using grep for its side effects is only a best practice, so yes, I think that people are using it for its side effects, and that it would be a breaking change in enough programs to make it unfeasible. Or, at least it would be a big enough change and break from tradition that you'd never convince P5P to make the change :-) What's wrong with List::Util?

  • Comment on Re: Shortcutting grep in boolean context

Replies are listed 'Best First'.
Re^2: Shortcutting grep in boolean context
by perlancar (Hermit) on Jan 03, 2017 at 21:33 UTC
    As the list gets larger, the overhead of copying the list into @_ increases.
        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-24 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found