use strict; use Benchmark qw(timethese cmpthese); my @array = ( 'four', 'three', 'three|four', 'two', 'two|four', 'two|three', 'two|three|four', 'one', 'one|four', 'one|three', 'one|three|four', 'one|two', 'one|two|four', 'one|two|three', 'one|two|three|four' ); timethese( 10000, {'Schwartzian' => '&Schwartzian()', 'Orcish' => '&Orcish()', 'Direct Sort' => '&DirectSort()', } ); cmpthese( 10000, {'Schwartzian' => '&Schwartzian()', 'Orcish' => '&Orcish()', 'Direct Sort' => '&DirectSort()', } ); sub Schwartzian() { my @sorted_array = map {$_->[1]} sort {$b->[0] <=> $a->[0]} map {[length($_), $_]} @array; } sub Orcish() { my %m; my @sorted_array = sort { ($m{$b} ||= length($b)) <=> ($m{$a} ||= length($a)) } @array; } sub DirectSort() { my @sorted_array = sort {length $b <=> length $a} @array; }