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.1
Guillem Jover 6 years ago committed by Rene Krenn
parent 1de487b529
commit 117d346b76

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

@ -16,7 +16,7 @@
use strict;
use warnings;
use Test::More tests => 25;
use Test::More tests => 33;
require_ok('NGCP::BulkProcessor::Array');
@ -25,6 +25,7 @@ NGCP::BulkProcessor::Array->import(qw(
mergearrays
reversearray
removeduplicates
itemcount
));
# 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 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(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