I have been sorting some large arrays of numbers (up to a few million), and I noticed that the program takes a long time to "clean up" after it finishes.

Update - Perhaps I posted too soon. I've noticed that this problem doesn't happen with perl 5.8.3, but it does with 5.8.0 on one machine but not on another. Perhaps other monks can try it with different versions of perl for comparison?

Some minimal sample code:

use List::Util 'shuffle'; use Time::HiRes qw(gettimeofday tv_interval); my $N = 500_000; my $t1 = [gettimeofday]; my @a = (1 .. $N); prof("array created"); my @b = shuffle @a; prof("array shuffled"); my @c = sort {$a <=> $b} @b; prof("array sorted"); sub prof{ my $name = shift; my $t2 = [gettimeofday]; my $delta = tv_interval($t1, $t2); print "$delta\t$name\n"; $t1 = $t2; }

Which gives this output:

$ time ./t2.pl 
0.268895        array created
0.359648        array shuffled
2.382604        array sorted

real    0m22.609s
user    0m21.820s
sys     0m0.220s

Noticed that the real time is 22 s, which means that the proccess finished almost 20 s after it finished sorting (that's why I call this a "clean-up" delay for lack of a better name. I don't know how the internals work, but this problem doesn't happen when I don't sort. If I remove the sort line, I get:

0.488084        array created
0.739037        array shuffled

real    0m1.547s

Even if I increase $N to 5_000_000, the cleanup time when not sorting is reasonably fast:

2.729688        array created
4.622434        array shuffled

real    0m8.948s

Anyone has any idea about why this happens? I'm using perl 5.8.0 on linux.


In reply to Sort taking a long time to clean up by itub

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.