Schwartzian transform

Aaron Crane perl at aaroncrane.co.uk
Wed Aug 13 13:03:00 BST 2014


Damian Conway <damian at conway.org> wrote:
> 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.

Indeed. But if N is sufficiently large, it's probably worth using
Sort::Key over either the plain hash-ref version or the ST version:

use Sort::Key qw<nkeysort>;
my @sorted = nkeysort { $_->{position} } values %$ref;

I think that's still a lot clearer than the code you get with the ST
map/sort/map dance.

> As always, only benchmarking on real(istic) data can determine whether
> you will actually benefit from using the ST or not.

I agree wholeheartedly. In the absence of such benchmarking (and also
in the absence of any indication of a performance issue) I think the
approach of directly sorting the hash refs wins merely by virtue of
being so much simpler.

-- 
Aaron Crane ** http://aaroncrane.co.uk/


More information about the london.pm mailing list