PerlingTheUK has asked for the wisdom of the Perl Monks concerning the following question:
does what an equal SQL querymy $ret = $src_drinks->match( { sugar => "no", color => "black" } );
would do.SELELCT * FROM drinks WHERE sugar LIKE "no" AND color LIKE "black";
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?# 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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Object Matching, speed issue
by dragonchild (Archbishop) on Aug 25, 2004 at 18:41 UTC | |
by roju (Friar) on Aug 25, 2004 at 19:47 UTC | |
by PerlingTheUK (Hermit) on Aug 25, 2004 at 21:17 UTC | |
|
Re: Object Matching, speed issue
by revdiablo (Prior) on Aug 25, 2004 at 19:55 UTC | |
by PerlingTheUK (Hermit) on Aug 25, 2004 at 21:20 UTC | |
|
Re: Object Matching, speed issue
by fizbin (Chaplain) on Aug 25, 2004 at 22:07 UTC | |
by revdiablo (Prior) on Aug 26, 2004 at 17:40 UTC |