I thought I'd have a play, so here's my script - it will sort a file 'NUMBERS.DAT' containing lots of 8 digit decimal numbers into 'SORTED.DAT'

For interest's sake I've left all the temporary files in, and it assumes the input file is zero padded so all numbers are 8 digits, no more, no less.

It sorted a 20,000,000 line file (200MB) in 340 seconds (just over 5 minutes) on my PC never using more than 2MB RAM (well, it probably used more memory indirectly due to disk caching)

You would be able to get it quicker by making the 'radix' 100 instead of 10, it'd probably be twice as quick (heck, if you make the radix 100000000, it'll probably sort it in about 90 seconds, but you could run out of file handles ;-) )

$starttime = time; printf ("start - %d\n",$starttime); $pref = ""; for ($j = 0; $j < 10; $j++) { unlink "-$j"; } sortfile("numbers.dat", $pref, 7); for ($i = 6; $i >= 0; $i--) { for ($j = 0; $j < 10; $j++) { unlink "$pref-A-$j"; } for ($j = 0; $j < 10; $j++) { sortfile("$pref-$j", "$pref-A", $i); } $pref .= "-A"; } printf ("end sort - %d (%d)\n",time, time - $starttime); open (FILE, ">sorted.dat") || die; for ($i = 0; $i < 10; $i++) { open(IN, "$pref-$i") || die; while(<IN>) { print FILE $_; } close(IN); } close(FILE); printf ("end - %d (%d)\n",time, time - $starttime); sub sortfile { my ($source, $pref, $offset) = @_; my $i, @fh; printf("$offset $pref $source - %d\n", time - $starttime); open(FILE, $source) || die; for ($i = 0; $i < 10; $i++) { open($fh[$i], ">>$pref-$i") || die; } while(<FILE>) { $f = $fh[substr($_, $offset, 1)]; print $f $_; } close(FILE); for ($i = 0; $i < 10; $i++) { close($fh[$i]); } }
(PS - it's actually quite a simple algorithm to understand as well, even I can follow it ;-) )

The algorithm will be able to sort much bigger files in linear time, and without using any more memory


In reply to Re: Re: Re: Re: Re: Re: Re: (Guildenstern) Re: Re: Taming a memory hog by Paul Smith
in thread Taming a memory hog by Guildenstern

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.