nysus has asked for the wisdom of the Perl Monks concerning the following question:
I've been kicking around a sort routine for fun:
The above code sorts an array of hash containg 3 elements in the following order: IP address, Date, Time. It sorts approx. 17,000 entries. I benchmarked this sucker and it weighed in at a whopping 2 min. 38 sec. for one iteration. Then, on Benchmarking your code's suggestion, I changed@sorted_by_ip = sort { my @a = $a->{ip} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/; my @b = $b->{ip} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/; $a[0] <=> $b[0] || $a[1] <=> $b[1] || $a[2] <=> $b[2] || $a[3] <=> $b[3] || $a->{date} cmp $b->{date} || $a->{time} cmp $b->{time} } @ip_date_time;
So there is my Perl epiphany for the day. If anyone knows, I'd love to hear why it made such a huge difference in this particular case. I can only vaguely guess why.
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot";
$nysus = $PM . $MCF;
Click here if you love Perl Monks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Speeding up sort routines
by davorg (Chancellor) on Jul 02, 2001 at 19:42 UTC | |
by arhuman (Vicar) on Jul 02, 2001 at 21:30 UTC | |
by petral (Curate) on Jul 02, 2001 at 23:19 UTC | |
by nysus (Parson) on Jul 02, 2001 at 19:45 UTC | |
by davorg (Chancellor) on Jul 02, 2001 at 19:59 UTC | |
by Anonymous Monk on Jul 02, 2001 at 20:31 UTC | |
by lhoward (Vicar) on Jul 02, 2001 at 19:53 UTC | |
by Anonymous Monk on Jul 02, 2001 at 20:00 UTC | |
by bwana147 (Pilgrim) on Jul 02, 2001 at 20:07 UTC | |
|
Re: Speeding up sort routines
by bwana147 (Pilgrim) on Jul 02, 2001 at 20:02 UTC |