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

Re^3: How to check if a variable's value is equal to a member of a list of values

by dbuckhal (Pilgrim)
on Mar 26, 2013 at 21:29 UTC ( #1025589=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to check if a variable's value is equal to a member of a list of values
in thread How to check if a variable's value is equal to a member of a list of values

Just to toss in a tidbit, and not trying to hijack the thread, but while benchmarking an alternative to using the smart match operator, I discovered that using map in scalar content performed better than grep.
if( map { /$string/ } @array) {
rather than...
if( grep { /$string/ } @array) {

I was surprised, because either way you'd be getting a non-undef value to satisfy the conditional, but map did it faster. I can post my code in a new thread if anyone's interested, otherwise, carry on! :)

Replies are listed 'Best First'.
Re^4: How to check if a variable's value is equal to a member of a list of values
by LanX (Chancellor) on Mar 26, 2013 at 21:37 UTC
    grep { TEST } LIST is semantically equivalent to map { TEST ? $_ : () } LIST

    Seems obvious that more logic results in longer runtime.

    Don't you think so?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Yes, I would agree that more logic increases runtime, but from your example, are you saying that grep should be faster? Please explain your example, so I understand you clearly.

      Personally, I have no idea how grep, map, or ~~ all work "under the hood". In your syntax, I see grep and map both iterating over the list, so my guess was they would have performed equally. I was surprised at my results.
        > are you saying that grep should be faster?

        no I'm showing that grep {TEST} is more complex than map {TEST}

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025589]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2016-12-05 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (80 votes). Check out past polls.