Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
When working with a large file, don't read the whole file into an array. Process the file line by line. Replace
foreach (@list)
by
while (<>)

Also, when populating the hash, Perl can do most of the work for you:

push @{ $ipURL{$ip} }, $url;
This line can replace the whole if..else... construct. If the key doesn't exist in the hash, it will be created, and the dereference will create an anonymous array to which the url will be pushed. If the key exists, the url is simply pushed to the referenced array.

Whether it's also responsible for the slowdown, I have no idea.

Another place where you can avoid creating a temporary variable is the splitting. You can populate the two variables directly by subscripting the result of split:

my ($ip, $url) = (split /,/)[7, 31];

I used the following to generate the input:
perl -wE 'for (1..10000) {say join ",", map int rand 1000, 1..35}' > i +nput

This was the benchmark:

#!/usr/bin/perl use strict; use warnings; open my $FH, '<', 'input' or die $!; sub slow { my ($fh) = @_; seek $fh, 0, 0; my @list = <$fh>; my $linecounter; my %ipURL; foreach (@list) { $linecounter++; my @message=split(',',$_); my $ip=$message[7]; my $url=$message[31]; if (!(exists $ipURL{$ip})) { my @urlList; push(@urlList,$url); $ipURL{$ip}= \@urlList; } else { my @urlList=@{$ipURL{$ip}}; push (@urlList,$url); $ipURL{$ip}=\@urlList; } if (!($linecounter % 50000)) { print "Lines: $linecounter\n"; } } return \%ipURL } sub fast { my ($fh) = @_; my %ipURL; seek $fh, 0, 0; while (<$fh>) { my ($ip, $url) = (split /,/)[7, 31]; push @{ $ipURL{$ip} }, $url; } return \%ipURL } use Test::More tests => 1; is_deeply slow($FH), fast($FH), 'same'; use Benchmark qw{ cmpthese }; cmpthese(-3, { slow => sub { slow($FH) }, fast => sub { fast($FH) }, });

This is the result on my machine:

1..1 ok 1 - same Rate slow fast slow 20.7/s -- -26% fast 27.9/s 35% --

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: Hash Search is VERY slow by choroba
in thread Hash Search is VERY slow by rtjensen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-19 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found