For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > July 2004 > sort









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author sort
Larry

2004-07-15, 3:57 am

Hi Folks!

I'm fairly new at Perl and I'm having a lot of trouble with this line:

my @SORTED_ARRAY = sort { $HASH{$b} <=> $HASH{$a} } keys %HASH;

%HASH has got 100.000 keys and it takes 42sec to do the sort...I'm on a
G3 300Mhz MacOsX (%HASH is from a .dbm file)

That's because i'm writing a simple forum...I need to sort the Topics
according to their time...here's an example:

%HASH{$topic_id} = time;

can anyone help me?
Carsten Aulbert

2004-07-15, 3:57 am

Hi Larry,

Larry wrote:

> I'm fairly new at Perl and I'm having a lot of trouble with this line:
>
> my @SORTED_ARRAY = sort { $HASH{$b} <=> $HASH{$a} } keys %HASH;
>
> %HASH has got 100.000 keys and it takes 42sec to do the sort...I'm on a
> G3 300Mhz MacOsX (%HASH is from a .dbm file)
>
> That's because i'm writing a simple forum...I need to sort the Topics
> according to their time...here's an example:
>
> %HASH{$topic_id} = time;
>
> can anyone help me?


What exactly is your problem?

(1) %HASH{$topic_id} = time;
With 99% certainty you meant $HASH{$topic_id}=time;

(2) slow sort:

It might be a problem, if the hash is still partly on disk and is not fully
loaded into the memory. Just run the following small script:

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;
print "Filling hash\n";
my %hash = ();
for(0..1000000) {
$hash{$_} = rand(1000);
}

print "Sorting\n";
my $start = time();
my @array = sort {$hash{$b} <=> $hash{$a}} keys %hash;
my $stop = time();

print "Finished after ", $stop-$start, " seconds\n";
__END__

This one generates a few entries and sorts them. On my old P-III (700 MHz)
laptop it took 48 seconds, but I used 10 times as many entries as you used
(but I ran into swap space eventually :(). With 100000 entries it took
merely 4 seconds.

Please have a look at your disk activity while sorting the keys, this might
be a reason why it is so slow.

HTH

Carsten
John W. Krahn

2004-07-15, 8:56 am

Larry wrote:
>
> I'm fairly new at Perl and I'm having a lot of trouble with this line:
>
> my @SORTED_ARRAY = sort { $HASH{$b} <=> $HASH{$a} } keys %HASH;
>
> %HASH has got 100.000 keys and it takes 42sec to do the sort...I'm on a
> G3 300Mhz MacOsX (%HASH is from a .dbm file)
>
> That's because i'm writing a simple forum...I need to sort the Topics
> according to their time...here's an example:
>
> %HASH{$topic_id} = time;
>
> can anyone help me?


When you say "%HASH is from a .dbm file", does that mean that it is a
tied hash? This should be a bit faster as it doesn't have to access
%HASH in the sort code block:

my @SORTED_ARRAY = sort { $b <=> $a } values %HASH;


John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com