The performance problem all sounds to me like you have an insane number of words being passed to sort. Unfortunately, sort takes a list, and that list has to be on Perl's argument stack all at once. But, you should double-check that: print the count of words at the end of the script, for reference. Also you should check your system memory usage as it runs; if the script runs you into disk swap, then yes it is going to run very slowly.

I believe there is actually an internal optimization for sorting an array in-place, so @foo= sort { ... } @foo; should execute faster than your example because the array never actually goes onto the perl argument stack. Of course, that doesn't really fit your data structure too well.

You could try emptying the hash to free up memory using undef %f; after you pull out all the keys and values. That should cut your ram usage by half before the sort step. (but maybe too late to avoid running into disk swap)

I'd be curious if you could make use of Tree::RB::XS here, to avoid ever having to expand a full list of the items:

use Tree::RB::XS; for(@files){ open IN, $_; while(<IN>){ /^( [^\t\n]+ )\t( [0-9]+ )$/x; # avoid backtracking $f{$1}+=$2; } } my $t= Tree::RB::XS->new(compare_fn => 'int', allow_duplicates => 1); while (my ($word, $count)= each %f) { $t->insert($count, $word); } my $iter= $t->iter; printf OUT "%s\t%s\n", reverse $iter->next_kv while !$iter->done;

I apologize that I'm too lazy to generate some hundred-gigabyte files to benchmark this theory on my own.

If none of these keep the memory usage low enough to be useful, I would suggest trying a database. You could then run the query that sorts them and iterate the rows using DBI. There's a huge overhead in putting them into a database, but if you re-use this data then the database index would save you a lot of computing power in the long run.


In reply to Re: Long list is long by NERDVANA
in thread Long list is long by Chuma

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.