in reply to Re: Sorting an array of arrays by field
in thread Sorting an array of arrays by field

Your benchmark is unfair as the key extraction for ambrus solution is not being measured.

Anyway, using Sort::Key is, as usual, the fastest solution!

use Sort::Key 'ikeysort'; my @d = <DATA>; my @e; use Benchmark qw/cmpthese/; cmpthese (4e4, { grt => sub { @e = map { substr $_, 6 } sort map { my $val = (split /\|/ +)[4]; sprintf "%06d$_",$val} @d; }, raw => sub { @e = sort { (split '\|', $a, 12)[4] < +=> (split '\|', $b, 12)[4] } @d; }, ambrus => sub { my @key = map { (split /\|/)[4] } @d +; @e = @d[ sort { $key[$a] <=> $key[$b +] } 0 .. @d - 1 ]; }, sk => sub { @e = ikeysort { (split '\|', $_, 12)[ +4] } @d } });
on my computer says...
Rate raw grt ambrus sk raw 1887/s -- -63% -69% -74% grt 5141/s 172% -- -15% -29% ambrus 6061/s 221% 18% -- -16% sk 7246/s 284% 41% 20% --