foreach (@list)
####
while (<>)
##
##
push @{ $ipURL{$ip} }, $url;
##
##
my ($ip, $url) = (split /,/)[7, 31];
##
##
perl -wE 'for (1..10000) {say join ",", map int rand 1000, 1..35}' > input
##
##
#!/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) },
});
##
##
1..1
ok 1 - same
Rate slow fast
slow 20.7/s -- -26%
fast 27.9/s 35% --