Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

In reply to Re: Fast seeking in a large array of hashes to generate a report. by themage
in thread Fast seeking in a large array of hashes to generate a report. by jbrugger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (9)
    As of 2015-07-07 02:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (86 votes), past polls