http://www.perlmonks.org?node_id=1025309


in reply to Re: 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

Hi,

intersting: Because I'm curious: Is this substantially different (memory footprint, performance) than:

if(grep { $_ eq $string } @array) {

McA

  • Comment on Re^2: How to check if a variable's value is equal to a member of a list of values
  • Download Code

Replies are listed 'Best First'.
Re^3: How to check if a variable's value is equal to a member of a list of values
by LanX (Saint) on Mar 25, 2013 at 14:28 UTC
    It should

  • smartmatch is implemented in C
  • grep doesn't stop after matching.

    Unfortunately all the use cases of smartmatch are hard to remember.

    For repeated lookups a prepared hash scales certainly better.

    You're free to check this with benchmarks or search for older discussions.¹

    I hoped smartmatch could at least handle the stringification limitation of hashes, but nope:

    DB<141> $h1={} => {} DB<142> $h1 ~~ [$h1] => "" DB<143> 5 ~~ [5] => 1

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    Update

    ¹) Brian got deep into benchmarking :) http://stackoverflow.com/questions/3951812/how-fast-is-perls-smart-match-operator-for-searching-scalar-in-an-array

      Thank you. An intersting link. I have to admit that I haven't worked with the smart match operator. It's a little bit too much "Eierlegendenwollmilchsau" (1) for me. And what I read about it confirmed to wait with it. But probably I'm too old fashioned.

      McA

      (1) Best translation I could find on dict.leo.org: jack of all trades device

Re^3: How to check if a variable's value is equal to a member of a list of values
by dbuckhal (Chaplain) on Mar 26, 2013 at 21:29 UTC
    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! :)
      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.