sortmap.pl
#!/usr/bin/perl
use strict;
use warnings;
package col;
sub new {
my $self = {};
my $class = shift;
return bless( $self, $class );
}
sub largecollege {
return ( [ qw{ 1 william } ], [ qw{ 2 peterson } ], [ qw{ 3 bechnell } ],
[ qw{ 4 davidson } ], [ qw{ 5 abramson } ], [ qw{ 6 adamson } ],
[ qw{ 7 stevenson } ], [ qw{ 8 cowgill } ], [ qw{ 9 fredricks } ],
[ qw{ 10 tillman } ], [ qw{ 11 cowgill } ], [ qw{ 12 barbera } ],
[ qw{ 13 davis } ], [ qw{ 14 listman } ], [ qw{ 15 adams } ],
[ qw{ 16 edwards } ], [ qw{ 17 majowycz } ], [ qw{ 18 bennis } ],
[ qw{ 19 geoffries } ], [ qw{ 20 nevill } ], [ qw{ 21 cowen } ],
[ qw{ 22 hillard } ], [ qw{ 23 olislav } ], [ qw{ 24 diggory } ],
[ qw{ 25 ignazio } ], [ qw{ 26 peters } ], [ qw{ 27 evan } ],
[ qw{ 28 jenkins } ], [ qw{ 29 rogers } ], [ qw{ 30 friedman } ],
[ qw{ 31 keller } ], [ qw{ 32 sidaris } ], [ qw{ 33 johnson } ],
[ qw{ 4 davidson } ], [ qw{ 5 abramson } ], [ qw{ 6 adamson } ],
[ qw{ 7 stevenson } ], [ qw{ 8 cowgill } ], [ qw{ 9 fredricks } ],
[ qw{ 13 davis } ], [ qw{ 14 listman } ], [ qw{ 15 adams } ],
[ qw{ 16 edwards } ], [ qw{ 17 majowycz } ], [ qw{ 18 bennis } ],
[ qw{ 19 geoffries } ], [ qw{ 20 nevill } ], [ qw{ 21 cowen } ],
[ qw{ 22 hillard } ], [ qw{ 23 olislav } ], [ qw{ 24 diggory } ],
[ qw{ 25 ignazio } ], [ qw{ 26 peters } ], [ qw{ 27 evan } ],
[ qw{ 28 jenkins } ], [ qw{ 29 rogers } ], [ qw{ 30 friedman } ],
[ qw{ 31 keller } ], [ qw{ 32 sidaris } ], [ qw{ 33 johnson } ],
[ qw{ 4 davidson } ], [ qw{ 5 abramson } ], [ qw{ 6 adamson } ],
[ qw{ 7 stevenson } ], [ qw{ 8 cowgill } ], [ qw{ 9 fredricks } ],
);
}
sub smallcollege {
return (
[ qw{ 4 davidson } ], [ qw{ 5 abramson } ],
);
}
package main;
use Data::Dumper;
use Benchmark qw(:all);
my $s = col->new;
my $simple_small = sub { return sort { $a->[1] cmp $b->[1] } $s->smallcollege };
my $simple_large = sub { return sort { $a->[1] cmp $b->[1] } $s->largecollege };
my $simuni_small = sub { my %hash = (); return sort { $a->[1] cmp $b->[1] } grep { !$hash{$_->[1]}++ } $s->smallcollege };
my $simuni_large = sub { my %hash = (); return sort { $a->[1] cmp $b->[1] } grep { !$hash{$_->[1]}++ } $s->largecollege };
my $tonys_small = sub {
my @pre_sorted = $s->smallcollege;
my $unsorted = \@pre_sorted;
my @key_list = ();
my @sorted = ();
my %hash;
# make the AoA into a hash, keyed on [1], the name
foreach(@$unsorted){
$hash{$_->[1]} = $_->[0];
}
# copy the keys into a sorted array
@key_list = sort( keys(%hash));
# build the sorted AoA
foreach(@key_list){
my @temp_array = ( $hash{$_} , $_ );
push @sorted, \@temp_array;
}
return @sorted;
};
my $tonys_large = sub {
my @pre_sorted = $s->largecollege;
my $unsorted = \@pre_sorted;
my @key_list = ();
my @sorted = ();
my %hash;
# make the AoA into a hash, keyed on [1], the name
foreach(@$unsorted){
$hash{$_->[1]} = $_->[0];
}
# copy the keys into a sorted array
@key_list = sort( keys(%hash));
# build the sorted AoA
foreach(@key_list){
my @temp_array = ( $hash{$_} , $_ );
push @sorted, \@temp_array;
}
return @sorted;
};
print "First, results from running the routines 50000 times on a small data set:\n";
timethese( 50000, {
'Smp. sm' => \&$simple_small,
'Smp. Unq sm' => \&$simuni_small,
'Orig sm' => \&$tonys_small,
}
);
print "\nNow running them 10000 times on a larger data set:\n";
timethese( 10000, {
'Smp. lg' => \&$simple_large,
'Smp. Unq lg' => \&$simuni_large,
'Orig lg' => \&$tonys_large,
}
);