How fast is Perl's smartmatch operator when searching for a scalar in an array?

前端 未结 3 1595
南旧
南旧 2020-12-14 18:32

I want to repeatedly search for values in an array that does not change.

So far, I have been doing it this way: I put the values in a hash (so I have an array and a

3条回答
  •  太阳男子
    2020-12-14 19:13

    Fast for small numbers of potential matches, but not faster than the hash. Hashes are really the right tool for testing set membership. Since hash access is O(log n) and smartmatch on an array is still O(n) linear scan (albeit short-circuiting, unlike grep), with larger numbers of values in the allowed matches, smartmatch gets relatively worse.

    Benchmark code (matching against 3 values):

    #!perl
    use 5.12.0;
    use Benchmark qw(cmpthese);
    
    my @hits = qw(one two three);
    my @candidates = qw(one two three four five six); # 50% hit rate
    my %hash;
    @hash{@hits} = ();
    
    sub count_hits_hash {
      my $count = 0;
      for (@_) {
        $count++ if exists $hash{$_};
      }
      $count;
    }
    
    sub count_hits_smartmatch {
      my $count = 0;
      for (@_) {
        $count++ when @hits;
      }
      $count;
    }
    
    say count_hits_hash(@candidates);
    say count_hits_smartmatch(@candidates);
    
    cmpthese(-5, {
        hash => sub { count_hits_hash((@candidates) x 1000) },
        smartmatch => sub { count_hits_smartmatch((@candidates) x 1000) },
      }
    );
    

    Benchmark results:

                 Rate smartmatch       hash
    smartmatch  404/s         --       -65%
    hash       1144/s       183%         --
    

提交回复
热议问题