Dear Monks,
I have an object oriented data structure with singleton objects. These objects hold a number of database rows as as a kind of parent. I now want to create a function that matches such objects individually. For eaxample for a table with drinks I would like to get all drinks, that are without sugar but are black.
My database contains a couple of thoiusands of such objects. I have a function calles match that receives a hash with the column name and the content.
my $ret = $src_drinks->match( { sugar => "no", color => "black" } );
does what an equal SQL query
SELELCT * FROM drinks WHERE sugar LIKE "no" AND color LIKE "black";
would do.
My match function is as follows:
# sub match # return all objects that match the requested options sub match{ my ( $self, $hashref ) = @_; my @keys = keys %{ $hashref }; if ( ( @keys == 1 ) && defined( $self->{ _matched }->{ $keys[0]."@". +$hashref->{ $keys[0] } } ) ) { return $self->{ _matched }->{ $keys[0]."@".$hashref->{ $keys[0] } + }; } # STEP 2: now go through all objects and return ptr to list of match +ed objects or 0, if none found. my @ret; foreach my $obj ( @{ $self->get_data() } ) { my $equals = 1; foreach my $key ( @keys ){ my $s = "\$obj->get_".$key."()"; $equals = 0 unless ( $hashref->{ $key } eq eval( $s ) ); } push ( @ret, $obj ) if ( $equals == 1 ); } if ( @ret == 0 ){ return 0; } $self->{ _matched }->{ $keys[0]."@".$hashref->{ $keys[0] } } = \@ret + if ( @keys == 1 ); return \@ret; }
This is extremely slow. Due to hundreds of calls to eval. Does anyone have any ideas how to speed things up a bit, without changing the whole data structure?
As you can see I have speeded up the whole process by saving all "single-column" matches, this is currently not memory critical but might become so I might really need to get anything faster to come around getting rid of this performance bit either.

Cheers,
PerlingTheUK

In reply to Object Matching, speed issue by PerlingTheUK

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.