TT#69200 Reimplement itemcount() based on grep()

This is somewhat faster:

                Rate  for_case grep_case       for      grep
  for_case   53476/s        --      -32%      -42%      -50%
  grep_case  78125/s       46%        --      -15%      -27%
  for        91743/s       72%       17%        --      -14%
  grep      106383/s       99%       36%       16%        --

Change-Id: I7b44f8f3b2146971f7b38e0224295ab7d301c615
(cherry picked from commit 5d38bd26f7)
mr7.5.6
Guillem Jover 6 years ago committed by Rene Krenn
parent 88a372ef7e
commit f0ee2f0771

@ -57,26 +57,18 @@ sub removeduplicates {
} }
sub itemcount { sub itemcount {
my ($item, $array_ptr, $case_insensitive) = @_;
my ($item,$array_ptr,$case_insensitive) = @_; if (defined $array_ptr and ref $array_ptr eq 'ARRAY') {
my $itemcount = 0; if ($case_insensitive) {
if (defined $array_ptr and ref $array_ptr eq 'ARRAY') { my $lc_item = lc $item;
if ($case_insensitive) { return scalar grep { lc eq $lc_item } @{$array_ptr};
foreach my $element (@$array_ptr) { } else {
if (lc($element) eq lc($item)) { return scalar grep { $_ eq $item } @{$array_ptr};
$itemcount += 1;
}
}
} else {
foreach my $element (@$array_ptr) {
if ($element eq $item) {
$itemcount += 1;
} }
}
} }
}
return $itemcount;
return 0;
} }
sub grouparray { sub grouparray {

@ -16,7 +16,7 @@
use strict; use strict;
use warnings; use warnings;
use Test::More tests => 25; use Test::More tests => 33;
require_ok('NGCP::BulkProcessor::Array'); require_ok('NGCP::BulkProcessor::Array');
@ -25,6 +25,7 @@ NGCP::BulkProcessor::Array->import(qw(
mergearrays mergearrays
reversearray reversearray
removeduplicates removeduplicates
itemcount
)); ));
# Container functions # Container functions
@ -57,3 +58,13 @@ is_deeply(removeduplicates([ qw(Aa BB cC) ], 1), [ qw(aa bb cc) ]);
is_deeply(removeduplicates([ qw(aA BB Aa Cc aa) ], 1), [ qw(aa bb cc) ]); is_deeply(removeduplicates([ qw(aA BB Aa Cc aa) ], 1), [ qw(aa bb cc) ]);
is_deeply(removeduplicates([ qw(aA AA bB Bb CC cc) ], 1), [ qw(aa bb cc) ]); is_deeply(removeduplicates([ qw(aA AA bB Bb CC cc) ], 1), [ qw(aa bb cc) ]);
is_deeply(removeduplicates([ qw(AA bB Aa cc Bb CC) ], 1), [ qw(aa bb cc) ]); is_deeply(removeduplicates([ qw(AA bB Aa cc Bb CC) ], 1), [ qw(aa bb cc) ]);
is(itemcount(), 0);
is(itemcount(undef, []), 0);
is(itemcount('foo', [ qw(aa bb cc aa zz aa aa) ]), 0);
is(itemcount('AA', [ qw(aa bb cc aa zz aa aa) ]), 0);
is(itemcount('aa', [ qw(aa bb cc aa zz aa aa) ]), 4);
is(itemcount('foo', [ qw(aa bb cc aa zz aa aa) ], 1), 0);
is(itemcount('AA', [ qw(aA bb cc Aa zz AA aa) ], 1), 4);
is(itemcount('aa', [ qw(aa bb cc aa zz aa aa) ], 1), 4);

Loading…
Cancel
Save