The call to eval is unnecessary. Try this instead (no promises on speeding up the code, as I haven't profiled it, but at least this eliminates the eval):

foreach my $key ( @keys ){ my $meth = "get_$key"; $equals = 0 unless ( $hashref->{ $key } eq $obj->$meth() ); }

Update: a simple benchmark shows that this way is indeed much faster than using eval. Again, not having profiled the code, I'm not sure how representative this is, but it's interesting.

Here's the benchmark:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(timethese cmpthese); { package foo; sub get_foo { "@_" }; sub get_bar { "@_" } } my $puk = sub { for (qw(foo bar)) { my $code = "foo->get_$_(qq(bar))"; eval $code; } }; my $rev = sub { for (qw(foo bar)) { my $meth = "get_$_"; foo->$meth("bar"); } }; cmpthese timethese(-2, { rev => $rev, puk => $puk, });

And here are the results:

Benchmark: running puk, rev for at least 2 CPU seconds... puk: 2 wallclock secs ( 2.08 usr + 0.00 sys = 2.08 CPU) @ 16 +447.12/s (n=34210) rev: 3 wallclock secs ( 2.08 usr + 0.00 sys = 2.08 CPU) @ 21 +7107.21/s (n=451583) Rate puk rev puk 16447/s -- -92% rev 217107/s 1220% --

In reply to Re: Object Matching, speed issue by revdiablo
in thread 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.