Can anyone see how to improve the performance--I'd like an order of magnitude improvement if possible--of the following code?
Essentially, the state of the booleans in @bools will switch from false to true in some undefinable order. Each time one becomes true, it will affect the composite state of one or more sets in @sets. And if all the booleans referenced by any particular set are true, then I need to remove that set from @sets.
This code does a simple brute force search of all the sets and checks all the booleans referenced. I keep looking at this thinking that there should be some combination of hashes or heaps and/or references that would allow me to remove completed sets more efficiently, but I'm not seeing it.
Update:Corrected typos.
#! 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 @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; __END__ C:\test>junk27 Took 11.623000
In reply to Better algorithm or data structure? by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |