Reverse your data structures:
#! perl -slw use strict; use Data::Dump qw[ pp ]; use List::Util qw[ shuffle ]; use Time::HiRes qw[ time ]; our $N //= 1e3; our $S //= 1e4; our $Z //= 5; my @bools = (0) x $N; my @sets = map [ map int( rand $N ), 1 .. 1 + int( rand $Z ) ],1 .. $S; my @sets1 = map [@$_], @sets; my @someOrder = (shuffle 0 .. $N-1); my $start = time; for my $next ( @someOrder ) { $bools[ $next ] = 1; for my $set ( reverse 0 .. $#sets ) { if( grep( $bools[ $_ ], @{ $sets[ $set ] } ) == @{ $sets[ $set + ] } ) { splice @sets, $set, 1; } } } printf "Took %.6f\n", time() - $start; $start = time; my @sizes = map scalar(@$_), @sets1; my @revsets = map [], @bools; my %sets; for my $set_ix (0..$#sets1) { if (@{$sets1[$set_ix]}) { $sets{$set_ix} = $sets1[$set_ix]; for my $bool (@{$sets1[$set_ix]}) { push @{$revsets[$bool]}, $set_ix; } } } for my $next ( @someOrder ) { for my $set_ix (@{$revsets[$next]}) { unless (--$sizes[$set_ix]) { delete $sets{$set_ix}; } } } printf "Took %.6f\n", time() - $start;
and it runs as...
Took 10.198676 Took 0.112004

In reply to Re: Better algorithm or data structure? by salva
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.