Skip to content

Instantly share code, notes, and snippets.

@Woody2143
Created October 10, 2013 20:26
Show Gist options
  • Save Woody2143/6925004 to your computer and use it in GitHub Desktop.
Save Woody2143/6925004 to your computer and use it in GitHub Desktop.
Benchmark of array vs hash for unique values
#!/opt/perlbrew/perls/cdrtools/bin/perl
# THIS IS HIDEOUS CODE WRITTEN by MDW; blame him. :P
use warnings;
use strict;
use Benchmark qw(:all);
my %recordTypes = (
START => \&startRecords,
STOP => \&stopRecords,
ATTEMPT => \&attemptRecords,
INTERMEDIATE => \&intermediateRecords,
);
my @tnFields = qw(callingNumber calledNumber numberBeforeTranslations dialedNumber genericAddressParam);
timethese(
my $count,
{
'hash' => sub {
my %numbers;
while (<STDIN>) {
my $record = $_;
chomp $record;
my @F = split(',', $record);
my $fields = &{$recordTypes{$F[0]}}(\@F);
for my $key (@tnFields) {
$numbers{$fields->{$key}} = 1;
}
}
},
'array' => sub {
my @numbers;
while (<STDIN>) {
my $record = $_;
chomp $record;
my @F = split(',', $record);
my $fields = &{$recordTypes{$F[0]}}(\@F);
for my $key (@tnFields) {
push @numbers, $fields->{$key};
}
}
my %numberHash;
@numberHash{@numbers} = ();
},
}
);
#-------------------------------------------------------------------------------------------------
sub startRecords {
my ($fields) = @_;
my %cdrFields = (
callingNumber => $fields->[14],
calledNumber => $fields->[15],
numberBeforeTranslations => $fields->[18],
dialedNumber => $fields->[49],
genericAddressParam => $fields->[52],
ingressProtocol => $fields->[41],
egressProtocol => $fields->[54],
callDirection => $fields->[11],
ingressTgidName => $fields->[28],
egressTgidName => $fields->[53],
);
return \%cdrFields;
}
#----------------------------------------------------
sub stopRecords {
my ($fields) = @_;
my %cdrFields = (
callingNumber => $fields->[19],
calledNumber => $fields->[20],
numberBeforeTranslations => $fields->[23],
dialedNumber => $fields->[59],
genericAddressParam => $fields->[62],
ingressProtocol => $fields->[51],
egressProtocol => $fields->[68],
callDirection => $fields->[16],
ingressTgidName => $fields->[33],
egressTgidName => $fields->[67],
);
return \%cdrFields;
}
#----------------------------------------------------
sub attemptRecords {
my ($fields) = @_;
my %cdrFields = (
callingNumber => $fields->[16],
calledNumber => $fields->[17],
numberBeforeTranslations => $fields->[20],
dialedNumber => $fields->[52],
genericAddressParam => $fields->[55],
ingressProtocol => $fields->[44],
egressProtocol => $fields->[58],
callDirection => $fields->[13],
ingressTgidName => $fields->[30],
egressTgidName => $fields->[57],
#gsx_callid => $fields->[71],
);
return \%cdrFields;
}
#----------------------------------------------------
sub intermediateRecords {
my ($fields) = @_;
my %cdrFields = (
callingNumber => $fields->[16],
calledNumber => $fields->[17],
numberBeforeTranslations => $fields->[20],
dialedNumber => $fields->[45],
genericAddressParam => $fields->[48],
ingressProtocol => $fields->[37],
egressProtocol => $fields->[50],
callDirection => $fields->[13],
ingressTgidName => $fields->[30],
egressTgidName => $fields->[49],
#gsx_callid => $fields->[64],
);
return \%cdrFields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment