And the result is as follows on a 1.8GHz Pentium 4: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; }
It turns out that the Schwartzian sort is the slowest of all. (The Orcish Maneuver was put in out of curiosity to see how expensive is the length function.)Benchmark: timing 100000 iterations of Direct Sort, Orcish, Schwartzia +n... Direct Sort: 4 wallclock secs ( 4.00 usr + 0.02 sys = 4.02 CPU) @ 2 +4900.40/s Orcish: 10 wallclock secs ( 9.47 usr + 0.00 sys = 9.47 CPU) @ 10 +560.78/s Schwartzian: 13 wallclock secs (13.77 usr + 0.00 sys = 13.77 CPU) @ 7 +264.80/s Rate Schwartzian Orcish Direct Sort Schwartzian 7272/s -- -31% -71% Orcish 10561/s 45% -- -58% Direct Sort 24907/s 242% 136% --
In reply to Re: Re: Sort Array by length of Value
by Roger
in thread Sort Array by length of Value
by Emanuel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |