I'm not sure whether this would work as I haven't thought how to get the remaining "sets" back after culling but here goes. How about using the garbage collection built into Perl by turning your @bools into %bools keyed by the range 1 .. $N and the values being anonymous arrays containing references to any "sets" that contain the number in the key. By delete'ing any key in %bools that is in @someOrder you are left with only references to "sets" that have at least one number reamaining. The "sets" are array references that are now out of scope so once all references are gone they are garbage collected without the need to do a splice. You should then be able to recover the remaining "sets" by combing through %bools for unique array refs.

This idea may be cock-eyed because I don't have time to fully test it now but your code took about 25 seconds on my elderly laptop whereas this code

use strict; use warnings; use List::Util qw{ shuffle }; use Time::HiRes qw{ time }; our $N //= 1e3; our $S //= 1e4; our $Z //= 5; my %bools = map { $_ => [] } 1 .. $N; for my $set ( 1 .. $S ) { my $conts = []; push @{ $conts }, int rand $N for 0 .. int rand $Z; push @{ $bools{ $_ } }, $conts for @{ $conts }; } my @someOrder = shuffle 1 .. $N; my $start = time(); delete @bools{ @someOrder }; printf "Took %.6f\n", time() - $start;

takes about 15ms, although more time would be taken recovering the sets.

I hope I'm not just having a rush of blood and that this is actually helpful.

Cheers,

JohnGG


In reply to Re: Better algorithm or data structure? by johngg
in thread Better algorithm or data structure? by BrowserUk

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.