Rather than writing the array to disk, having sort read it from disk and write the results back to disk, and then reading them in; you can save a couple of steps by piping the data directly to sort using a piped open, and let it write the results to disk for you to read back. The following processes 10 million elements in around 3 minutes on my machine.

#! perl -slw use strict; use Benchmark::Timer; our $N ||= 1e6; my $T = new Benchmark::Timer; my @array; push @array, sprintf "Test%07d", int rand 32767 for 1 .. $N; $T->start( 'sort -u' ); open my $pipe, "| u:sort -u > temp.tmp" or die $!; print $pipe $_ for @array; close $pipe; open my $in, '<', 'temp.tmp' or die $!; my $i = 0; $array[ $i++ ] = $_ while <$in>; close $in; $#array = $i - 1; ## Corrected, with thanks to [johngg] $T->stop( 'sort -u' ); $T->report; printf "Array now contains %d uniq elements\n", scalar @array; __END__ [ 8:17:47.37] c:\test>551753-2 -N=1e7 1 trial of sort -u (183.281s total) Array now contains 32768 uniq elements

In theory, you could use IPC::Run3 to avoid hitting the disk at all, but then you have the problem of needing storage for both the input and output at the same time. The code above reads the data back into the original array and truncates the leftovers, so avoiding any extra memory growth.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Saving an array to a disk file by BrowserUk
in thread Saving an array to a disk file by Anonymous Monk

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.