Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Two dimensional sets intersection

by Not_a_Number (Parson)
on Apr 10, 2013 at 20:44 UTC ( #1028038=note: print w/ replies, xml ) Need Help??


in reply to Two dimensional sets intersection

No need to worry about narrowing your search or whatever. Hash lookups are fast.

The code below creates a hash for a 100 x 100 matrix. It also assumes that each matrix pair has a value (worst case, given your 'spec'). To simplify validation, I make this value 1, but changing it to any other number makes no difference to the time required for lookup.

Subsequently, I make another worst-case assumption, namely that your lists of search terms (what you call Subset 1 and Subset 2) are maximally large.

On my machine, it finishes before I can blink. Run it and see:

use strict; use warnings; # Create 100 x 100 datastructure: my @uc = ( 'AA' .. 'DV' ); my @lc = ( 'aa' .. 'dv' ); my %index; for my $k ( @uc ) { for my $v ( @lc ) { $index{$k}{$v} = 1; } } # Parse it: my $score; for my $k ( @uc ) { # or: for my $k ( @subset_1 ) for my $v ( @lc ) { # or: for my $v ( @subset_2 ) $score += $index{$k}{$v} || 0; } } print $score;


Comment on Re: Two dimensional sets intersection
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1028038]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-12-18 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls