Subject: | Quite slow |
Array::OneOf performs quite poorly speed-wise compared to other solutions.
A benchmark script is attached to this bug report comparing five
different techniques for determining if an element is in an array.
Array::OneOf is the slowest.
Perl's built-in smart match operator is the fastest by far, about 14
times the speed on my machine. However, it may not always have the
desired effect - for example "0000" matches "0".
Subject: | any.pl |
use v5.14;
use Benchmark qw( :all );
use Test::More tests => 5;
use Array::OneOf qw( oneof );
use List::MoreUtils qw( any );
use Syntax::Keyword::Junction any => { -as => 'j_any' };
my @array = 'a' .. 'z';
my %solutions = (
array_oneof => sub { oneof 'n', @array },
junction => sub { 'n' eq j_any(@array) },
grep => sub { grep { $_ eq 'n' } @array },
any => sub { any { $_ eq 'n' } @array },
smart_match => sub { 'n' ~~ @array },
);
for my $s (sort keys %solutions) {
ok $solutions{$s}->(), "$s works";
}
diag "Benchmarks (high numbers are fast)";
my $timings = timethese -1, \%solutions, 'none';
for my $s (sort keys %$timings) {
diag sprintf("%-12s : %d", $s, $timings->{$s}[5]);
}