in reply to Re^3: matching values in arrays
in thread matching values in arrays
It uses pretty little space (not just in this particular example, but in general...) considering what's doing... Think of it as an index on the joins of the three arrays...
For really big arrays using greps on every iteration would make the operation really cumbersome and memory would be used anyway for the temporaries for grep on each of the arrays... This approach allows logarithmic access to the key once the index is done, yours would be at least of order O(N^2)...
Example:
# some are shared by others... @array1 = (1 .. 100001); @array2 = (100001 .. 200001); @array3 = (200001 .. 300000); for $i ( 1 .. 100000, 1 .. 10000 ) { $a = int( rand( 3 ) ) + 1; # dark magic, beware... push @array4, ${*{"array$a"}}[$i]; } my %which; do { push @{ $which{$_} } => "array1" } for @array1; do { push @{ $which{$_} } => "array2" } for @array2; do { push @{ $which{$_} } => "array3" } for @array3; my @pairs = @array4; while (my ($one,$two) = splice(@pairs,0,2)) { print "." if "$one and $two are in (@{$which{$one}}) and (@{$which{ +$two}}) respectively\n"; } __END__ This renders (on my box): 4.05user 0.15system 0:04.31elapsed 97%CPU (0avgtext+0avgdata 0maxresid +ent)k 0inputs+0outputs (0major+22995minor)pagefaults 0swaps
with bigger arrays it will grow bigger, of course, but if you have that many data you will have a lot of RAM... memory is cheap nowadays, cheaper than CPU cycles, I think...
best regards
--
our $Perl6 is Fantastic;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: matching values in arrays
by Animator (Hermit) on Dec 14, 2004 at 18:23 UTC | |
|
Re^5: matching values in arrays
by Animator (Hermit) on Dec 14, 2004 at 18:50 UTC |