in reply to Re: Numeric Sort for Stringified Value (How to Avoid Warning) (Use the ST)
in thread Numeric Sort for Stringified Value (How to Avoid Warning)
A bit over-engineered in this case. Take the same assumption as yours, the code could be much simpler:
my @new = sort {(split /\s+/, $b)[0] <=> (split /\s+/, $a)[0]} @old;
And it is faster:
use Data::Dumper; use strict; use warnings; my @old = ("10.5 AA", "10.6 AA", "9 AC", "2 BB"); my $t0 = time(); for (1..200000) {#yours my @new = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, (split( /\s+/, $_, 2 ))[0] ] } @old; } print time() - $t0, "\n"; $t0 = time(); for (1..200000) {#mine my @new = sort {(split /\s+/, $b)[0] <=> (split /\s+/, $a)[0]} @ol +d; } print time() - $t0, "\n";
I ran four times, yours took: 13, 14, 14, 13 seconds, when mine took 8, 10, 11, 9 seconds.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Numeric Sort for Stringified Value (How to Avoid Warning) (Use the ST)
by bobf (Monsignor) on Sep 16, 2005 at 07:08 UTC | |
|
Re^3: Numeric Sort for Stringified Value (How to Avoid Warning) (Use the ST)
by ikegami (Patriarch) on Sep 16, 2005 at 07:10 UTC |