package Person {
sub new{ my $p = shift; bless { @_ }, $p }
sub name{ $_[0]->{name } }
sub title{ $_[0]->{title} }
}
####
use v5.14;
use strict;
use warnings;
use List::MoreUtils qw( part );
use Benchmark ':all';
package Person {
sub new{ my $p = shift; bless { @_ }, $p }
sub name{ $_[0]->{name } }
sub title{ $_[0]->{title} }
}
my @employees = map {
Person::->new(
name => int(rand(999999)),
title => int(rand(2))?'Staff':'Manager',
)
} 0..200;
sub obvious {
my @sorted = sort {
if ($a->title =~ /Manager/ and not $b->title =~ /Manager/) {
-1;
}
elsif ($b->title =~ /Manager/ and not $a->title =~ /Manager/) {
1;
}
else {
$a->name cmp $b->name;
}
} @employees;
}
sub subtle {
my @sorted = sort {
($a->title !~ /Manager/) - ($b->title !~ /Manager/)
or $a->name cmp $b->name;
} @employees;
}
sub functional {
my @sorted =
map { sort { $a->name cmp $b->name } @$_ }
part { $_->title !~ /Manager/ }
@employees;
}
sub x {
my @sorted = map $_->[1],
sort{ $a->[0] cmp $b->[0] } map[
$_->title =~ /Manager/ ? 'A'.$_->name : 'B'.$_->name, $_
], @employees;
}
cmpthese( -1, {
obvious => \&obvious,
subtle => \&subtle,
functional => \&functional,
x => \&x,
});
####
C:\test>date /t & time/t && junk87
24/10/2012
19:39
Rate obvious subtle functional x
obvious 189/s -- -16% -60% -83%
subtle 226/s 19% -- -52% -80%
functional 471/s 149% 109% -- -57%
x 1101/s 482% 388% 134% --