Here's my stab at it, which goes along with what
jethro and
graff have already mentioned.
%sets is kept as a hash for convenient
deletes. By keeping track of which sets to update/delete once a resource (
$next) becomes available, we only need to traverse a minimal amount of sets:
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;
my %sets = map {
my $s = $_;
my $n = 1 + int( rand $Z );
++$bools[ $_ ]{ $s } for map int( rand $N ), 1 .. $n;
$s => $n;
} 0 .. $S - 1;
my @someOrder = shuffle 0 .. $N-1;
my $start = time;
for my $next ( @someOrder ) {
for my $s (keys %{ $bools[ $next ] }) {
$sets{ $s } -= delete $bools[ $next ]{ $s };
delete $sets{ $s } unless $sets{ $s };
}
}
printf "Took %.6f\n", time() - $start;
pp \@bools;
pp \%sets;
__END__
Took 0.089563
[
{},
{},
...
]
{}
Update: FWIW, the OP code took around 20 seconds on my virtual machine running FreeBSD.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.