Hello melmoth,
Here’s a more Perlish version of your code, using Set::Tiny for the convenience of its is_subset method:
use strict; use warnings; use Set::Tiny 'set'; my @paths = ( [ qw(A B F G) ], [ qw(A B) ], [ qw(A B X) ], [ qw(A B C D E F G) ], [ qw(I J K L M) ], ); my @sets; push @sets, set(@$_) for @paths; @sets = sort { $a->size <=> $b->size } @sets; # UPDATED my @filtered; LINE: for my $i (0 .. $#sets) { my $set_i = $sets[$i]; for my $j ($i + 1 .. $#sets) { next LINE if $set_i->is_subset($sets[$j]); } push @filtered, $set_i; } print $_->as_string, "\n" for @filtered;
<Update> Added sort line marked # UPDATED as per BrowserUk’s post below. </Update>
Now (other things being equal), the larger the set J, the more likely it is that J is a superset of a given set I. So an obvious speedup to try is to test I against the larger sets first, by reverseing the inner loop:
for my $j (reverse($i + 1 .. $#sets))
Of course, you’ll need to Benchmark the code to determine whether this makes a significant difference for your input data.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: Fast Way to find one array in second array
by Athanasius
in thread Fast Way to find one array in second array
by melmoth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |