As usually, if speed is your concern, you can trade it for space.
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
warn "start\n";
my $tstart1 = time;
my (%by_count, %by_word);
while (<>) {
chomp;
my ($k, $v) = split /\t/, $_, 2;
if (exists $by_word{$k}) {
$v += $by_word{$k};
delete $by_count{ $by_word{$k} }{$k};
}
undef $by_count{$v}{$k};
$by_word{$k} = $v;
}
my $tend1 = time;
warn "get properties: ", $tend1 - $tstart1, " secs\n";
my $tstart2 = time;
for my $count (sort { $b <=> $a } keys %by_count) {
say "$_\t$count" for sort keys %{ $by_count{$count} };
}
my $tend2 = time;
warn "sort + output: ", $tend2 - $tstart2, " secs\n";
warn "total: ", $tend2 - $tstart1, " secs\n";
Comparison?
llil start
get_properties : 13 secs
sort + output : 85 secs
total : 98 secs
start
get properties: 21 secs
sort + output: 25 secs
total: 46 secs
The output is identical.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
-
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.