in reply to Object Matching, speed issue

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% --

Replies are listed 'Best First'.
Re^2: Object Matching, speed issue
by PerlingTheUK (Hermit) on Aug 25, 2004 at 21:20 UTC
    I was not aware you can do that. Thank you, great help.

    Cheers,
    PerlingTheUK