use warnings;
use strict;
use Time::HiRes qw/time/;
my $common = join "","a".."z";
my @x = map { $common . rand 1 } 1..1e6;
my (@a,$a,$b,$start);
print "\n--- with full sort\n";
@a=@x;
$start=time();
my @b= sort @a;
print $b[0],"\n",$b[-1],"\n";
print time -$start,"\n";
print "\n--- with triple sort\n";
@a=@x;
$start=time();
$a= shift @a;
$b= shift @a;
($a,undef,$b) = sort($a,$_,$b) for @a;
print $a,"\n",$b,"\n";
print time -$start,"\n";
print "\n--- with assignment\n";
@a=@x;
$start=time();
$a= shift @a;
$b= shift @a;
for my $x (@a) {
$a = ($x,$x,$a)[$a cmp $x];
#next if $a eq $x;
$b = ($x,$b,$x)[$b cmp $x];
}
print $a,"\n",$b,"\n";
print time -$start,"\n";
print "\n--- with goto\n";
@a=@x;
$start=time();
$a= shift @a;
$b= shift @a;
for my $x (@a) {
goto ("NEXT", "NEWMIN", "MAYBEMAX")[$a cmp $x];
NEWMIN:
$a=$x;
next;
MAYBEMAX:
goto ("NEXT", "NEXT", "NEWMAX" )[$b cmp $x];
NEWMAX:
$b=$x;
NEXT:
}
print $a,"\n",$b,"\n";
print time -$start,"\n";
####
--- with full sort
abcdefghijklmnopqrstuvwxyz0.000100396248079448
abcdefghijklmnopqrstuvwxyz9.99150346814304e-06
7.66956496238708
--- with triple sort
abcdefghijklmnopqrstuvwxyz0.000100396248079448
abcdefghijklmnopqrstuvwxyz9.99150346814304e-06
5.88848209381104
--- with assignment
abcdefghijklmnopqrstuvwxyz0.000100396248079448
abcdefghijklmnopqrstuvwxyz9.99150346814304e-06
1.2906858921051
--- with goto
abcdefghijklmnopqrstuvwxyz0.000100396248079448
abcdefghijklmnopqrstuvwxyz9.99150346814304e-06
2.99558115005493
####
print "\n--- with le/ge\n";
@a=@x;
$start=time();
$a= shift @a;
$b= shift @a;
for my $x (@a) {
$a = $x if $a ge $x;
$b = $x if $b le $x;
}
print $a,"\n",$b,"\n";
print time -$start,"\n";
__END__
--- with le/ge
abcdefghijklmnopqrstuvwxyz0.000100420329498974
abcdefghijklmnopqrstuvwxyz9.95282924378671e-05
0.773550033569336