You pays your money and takes your choice:
#! perl -slw
use strict;
use Benchmark qw[ cmpthese ];
use constant {
X => 0, Y => 1, Z => 2, P => 3, Q => 4,
};
use Readonly;
Readonly::Hash my %Fields => {
X => 0, Y => 1, Z => 2, P => 3, Q => 4,
};
sub prioritisedSort1 {
return $a->[ P ] <=> $b->[ P ]
or $a->[ Z ] <=> $b->[ Z ]
or $a->[ Y ] <=> $b->[ Y ]
or $a->[ X ] <=> $b->[ X ]
or $a->[ Q ] <=> $b->[ Q ];
}
sub prioritisedSort2 {
return $a->[$Fields{P}] <=> $b->[$Fields{P}]
or $a->[$Fields{Z}] <=> $b->[$Fields{Z}]
or $a->[$Fields{Y}] <=> $b->[$Fields{Y}]
or $a->[$Fields{X}] <=> $b->[$Fields{X}]
or $a->[$Fields{Q}] <=> $b->[$Fields{Q}];
}
our $N ||= 1e5;
my @AoA1 = map {
[ map{ int rand $_ } 1000, 100, 10, 500, 50 ]
} 1 .. $N;
my @AoA2 = map{ [ @$_ ] } @AoA1;
my( @sorted1, @sorted2 );
cmpthese -1, {
constant => sub{ @sorted1 = sort prioritisedSort1 @AoA1 },
Readonly => sub{ @sorted2 = sort prioritisedSort2 @AoA2 },
};
print "\nFirst and last 5 for constant";
print "[@$_]"
for @sorted1[ 0 .. 4 ], [ qw[ - - - - - ] ], @sorted1[ -5 .. -1 ];
print "\nFirst and last 5 for Readonly";
print "[@$_]"
for @sorted2[ 0 .. 4 ], [ qw[ - - - - - ] ], @sorted2[ -5 .. -1 ];
__END__
C:\test>junk8 -N=1e2
Rate Readonly constant
Readonly 137/s -- -96%
constant 3459/s 2427% --
First and last 5 for constant
[226 44 0 8 3]
[425 64 7 10 2]
[790 25 8 20 27]
[461 37 0 27 47]
[151 51 3 31 35]
[- - - - -]
[734 15 9 482 6]
[90 40 2 489 5]
[957 21 9 491 45]
[17 99 3 494 44]
[585 50 7 495 22]
First and last 5 for Readonly
[226 44 0 8 3]
[425 64 7 10 2]
[790 25 8 20 27]
[461 37 0 27 47]
[151 51 3 31 35]
[- - - - -]
[734 15 9 482 6]
[90 40 2 489 5]
[957 21 9 491 45]
[17 99 3 494 44]
[585 50 7 495 22]
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.