Re: Sorted Hash



"palexvs@xxxxxxxxx" <palexvs@xxxxxxxxx> wrote in news:a1ddaad2-89c6-4b01-
bd39-2e27e36ffe39@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:

I filled hash and then printed it (sorted by key):
my %hs = (
key10 => 5,
key5 => b,
aey9 => 7,
)
foreach my $k (sort keys %hs) { print "$k $hs{$k}\n"; }

key - string ([0-9A-F]{72}), 50K records.
How do it more effective?

Well, depends on what you mean by 'more effective'.

If you are talking about speed, note that the only place where you can
really get any meaningful improvement is IO.

I am using Windows XP SP2 with ActiveState Perl 5.8.8.822 on an Intel Core
Duo 1.66 Mhz with 1 GB physical memory with a whole bunch of other apps
open.

Let's start with:

#!/usr/bin/perl

use strict;
use warnings;

my %hash;

for my $k ( 1 .. 50_000 ) {
$hash{ make_key() } = $k;
}

### printing code here

sub make_key {
join('', map { sprintf( '%.16f', $_ ) } (rand, rand, rand, rand) );
}

__END__
C:\DOCUME~1\asu1\LOCALS~1\Temp\t> timethis t

TimeThis : Command Line : t
TimeThis : Start Time : Thu Nov 29 19:32:35 2007
TimeThis : End Time : Thu Nov 29 19:32:36 2007
TimeThis : Elapsed Time : 00:00:01.109

So, let's add printing to this script:

my @keys = sort keys %hash;
print "$_ $hash{$_}\n" for @keys;

TimeThis : Command Line : t
TimeThis : Start Time : Thu Nov 29 19:37:11 2007
TimeThis : End Time : Thu Nov 29 19:37:14 2007
TimeThis : Elapsed Time : 00:00:03.156

So, we are talking about 2.047 seconds added by printing the hash.

Assuming 80 bytes per print statement and 50,000 print statements, that's
3906.25 K of output in about two seconds.

That's not bad and does not leave a lot of room for improvement.

Following the printing late strategy and trying to trade-off memory for
speed, let's accumulate all output before printing:

my $out;
for ( @keys ) {
$out .= "$_ $hash{$_}\n";
}

{
local $| = 0;
print $out;
}

TimeThis : Command Line : t
TimeThis : Start Time : Thu Nov 29 19:56:33 2007
TimeThis : End Time : Thu Nov 29 19:56:39 2007
TimeThis : Elapsed Time : 00:00:05.859

Well, that wasn't good (and I really did not expect it to be). But we don't
have to accumulate all output. We can just print after every 9K or so. (I
came up with that magic number on my system after some trial and error ...
Time program, go do something in Word, load cnn.com on Firefox, go back and
re-time this script ... poor man's cache clearing).

my $out;
for ( @keys ) {
$out .= "$_ $hash{$_}\n";
if ( length $out > 9_000 ) {
print $out;
$out = q{};
}
}

TimeThis : Command Line : t
TimeThis : Start Time : Thu Nov 29 20:04:57 2007
TimeThis : End Time : Thu Nov 29 20:05:00 2007
TimeThis : Elapsed Time : 00:00:03.062

So, after all that tinkering, I got an improvement of 0.094 seconds, that
is about 3%.

Now, my against my gut feeling, I did try:

print "$_ $hash{$_}\n" for sort keys %hash;

TimeThis : Command Line : t
TimeThis : Start Time : Thu Nov 29 20:11:01 2007
TimeThis : End Time : Thu Nov 29 20:11:04 2007
TimeThis : Elapsed Time : 00:00:02.953

Hmmm ... It does not look like I can beat the simplest alternative by
trying to be clever.

That took 1.844 seconds to output about 3906.25K.

of course, I probably wasted my time because I misunderstood your vaguely
phrased question.

Sinan

--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>

.



Relevant Pages

  • [SLE] N-up printing in KDE with Cups, OpenOffice, etc (was Re: [SLE] pdf booklet)
    ... > to buy adobe acrobat? ... CUPS N-Up printing, or specifically 4-up printing selected from the ... Click the "Add filter" button - it looks like a funnel. ... Command"), and then click the "Edit Command" button on the "Command edit ...
    (SuSE)
  • RE: Problems Printing After XP Upgrade from 98SE
    ... In addition to not printing, ... all of the jobs from the queue and print a test page from XP, ... > Use the net.exe command to establish a persistent connection. ... > network printer path when the LPT port exists on the computer as a physical ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Get RAW Bitmap Data from a file
    ... Posiflex uses the Epson command set). ... I am using code from this MS Knowledgebase that allows printing RAW ... the Epson command to turn on Define Graphic Download). ... Any other format, even DONTCARE fails with ...
    (microsoft.public.dotnet.languages.vb)
  • Re: scheduled printing of a text file using batch files
    ... this would be a scheduled task question rather than a printing question. ... have never used the schtasks command. ...
    (microsoft.public.windowsxp.print_fax)
  • Re: Printing on a thermal printer
    ... I use a lot of Thermal Printers for labelling applications and have had the ... spooler API's and allows me to send command directly to the printer in its ... Simply set the printer name to the name of any installed windows ... actually control the printing of pages as the driver is being bypassed, ...
    (microsoft.public.dotnet.framework)