Schwartzian transform
Damian Conway
damian at conway.org
Wed Aug 13 12:45:59 BST 2014
Mark and Aaron are ntirely correct that the ST is not required for your example,
but if your actual application uses a significantly larger dataset than just
four hash entries, then the ST may still be preferable...as array lookups
are around twice as fast as hash lookups. That might be a significant
performance enhancement if you have to do O(NlogN) key look-ups for
a sufficiently large value of N.
As always, only benchmarking on real(istic) data can determine whether
you will actually benefit from using the ST or not.
And, FWIW, I'd have written the shorter version as:
my @sorted_records
= sort { $a->{position} <=> $b->{position} }
values %{$ref};
my $next_position = 1;
for my $next_record (@sorted_records) {
$next_record->{position} = $next_position++;
}
...to maximize long-term maintainability.
Damian
More information about the london.pm
mailing list