in reply to searching through data
What about getting the job done in less than a half of second? (no DBMs, no Hashes):
use strict; use warnings; my ($file1, $file2) = @ARGV; my $ids = 1000000; # last id my $bin=0; substr($bin,$_,1,pack ("c",0)) for (0..$ids); # Create the index open my $fh1, "<", $file1 or die $!; while (my $id = <$fh1>){ chomp $id; substr($bin, $id,1,pack ("c",1)); } close $fh1; # Search $ids from file 2 open my $fh2, "<", $file2 or die $!; while (my $id2 = <$fh2>){ chomp $id2; print "$id2\n" if ((unpack "c",substr ($bin,$id2,1)) == 1); } close $fh2;
file1 contains 400000 numbers between 0 and 1000000. file2 contains 10000 numbers in the same range. Processing both takes less than half a second.
time perl sp.pl file1 file2 > /dev/null real 0m0.448s user 0m0.448s sys 0m0.000s
A bit of explanation: The program constructs an scalar ($bin) containing 1000000 of 0s. While processing the index (the first list of ids), it substitutes every 0 at position $id with a 1.
Then, it reads the second file of ids and checks if at position $id2, the $bin scalar has a 0 (not seen previously) or a 1 ($id already seen in the first file).
Hope this helps
citromatik
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching through data
by almut (Canon) on Apr 16, 2009 at 14:31 UTC |