Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Fast seeking in a large array of hashes to generate a report.

by themage (Friar)
on Jun 23, 2005 at 11:34 UTC ( #469364=note: print w/ replies, xml ) Need Help??


in reply to Fast seeking in a large array of hashes to generate a report.

Hi ppl, Hi jbrugger, I created a small script that generates an array with 1.000.000 records as listed in your example as follow:

use strict; use Time::HiRes qw(time); my @data=(); my @names=qw(albert arthur bernard bill charlie david jack jonh joseph + mark michael peter steven); my @surnames=qw(bell brown canfield cornwell devlin doyle golden hoffm +an maclean powell rowling tovard twain warlick); my @places=qw(amsterdan athens belfast berlin bern brussels copenhagen + helsinki lisbon london luxenbourg madrid oslo paris rome stockholm v +aduz vienna); for (my $i=0;$i<=1000000;$i++) { my $name=$names[int(rand()*$#names)]; my $surname=$surnames[int(rand()*$#surnames)]; my $place=$places[int(rand()*$#places)]; my $age=int(rand()*50)+25; my $rec={name=>$name,surname=>$surname,place=>$place,age=>$age +}; push @data, $rec; }
Copied @data to @Persons and @Person2, and used the @Filters arrays as follow:
my @Fields=( { key => 'age', content=>35 }, { key => 'place', content=>'london'}, ); my @Persons=@data; my @Person2=@data; print "Persons: $#Persons\nPerson2: $#Person2\n\n"; my $field; my $person; my $i=0;
Timed your implementation with:
my $ta=time(); foreach $field (@Fields) { # <- age and surname in this case scalar(@Persons)==0 && last; for ($i=0;$i<scalar(@Persons);$i++) { ($Persons[$i]->{$field->{key}} eq $field->{content}) && next; splice(@Persons,$i,1); $i--; } } my $tb=time();
And then made a small change in your filtering algorith, so the main interaction is against the data, not against the filters as follow:
my $use=1; my @Rset=(); foreach $person (@Person2) { $use=1; foreach $field (@Fields) { if ($person->{$field->{key}} ne $field->{content}) { $use=0; last; } } if ($use) { push @Rset, $person; } } my $tc=time;
And then printed some stats:
my $iab=$tb-$ta; my $ibc=$tc-$tb; print "TIMES:\nab: $iab\nbc: $ibc\n"; print "COUNTS:\nab: ", $#Persons,"\nbc: ",$#Rset,"\n";
I was expecting some diference (not know better or worst, was just a test), and got some. Enexpected, I admit. In my P4 3.2Ghz, 1GB memory this are some results:
mpneves@voyager perl$ ./test.pl Persons: 1000000 Person2: 1000000 TIMES: ab: 17.5640239715576 bc: 1.96199607849121 COUNTS: ab: 1153 bc: 1153 mpneves@voyager perl$ ./test.pl Persons: 1000000 Person2: 1000000 TIMES: ab: 18.5665609836578 bc: 1.95595502853394 COUNTS: ab: 1185 bc: 1185 mpneves@voyager perl$ ./test.pl Persons: 1000000 Person2: 1000000 TIMES: ab: 24.4221110343933 bc: 3.58428502082825 COUNTS: ab: 1213 bc: 1213
Which means that my version is performing better at least 1 to 3 than yours. Obviously, this is a test in my notebok, with random data. with your computers, with real data and filters it can performe diferently. All code I used is here. If you join all code segments as they are presented you will have my complete code.


Comment on Re: Fast seeking in a large array of hashes to generate a report.
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-07-26 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls