in reply to Re^5: Using SUPER in dynamically generated subs
in thread Using SUPER in dynamically generated subs

Thanks ikegami - that works a treat.

When I benchmarked the three methods, I got this:

Rate super eval orig super 7794/s -- -88% -93% eval 67568/s 767% -- -35% orig 104167/s 1236% 54% --

... where super is using SUPER, eval is ikegami's method, and orig is the one in the original post.

The eval method would be preferable because it checks the @ISA at runtime, while my method, although faster, would only check @ISA at compile time.

UPDATE At ikegami's suggestion, I have added the benchmark code:

# Setup 3 methods find_ids1, find_ids2, find_ids3 ################################################### use SUPER; # Using a code ref extracted at compile time my $orig = $class->can('find_ids'); install_sub($class,'find_ids1', sub { my $class = shift; my %params = ref $_[0] ? %{ $_[0] } : (@_); foreach my $column (keys %params) { if (exists $alias_for->{$column}) { $params{$alias_for->{$column}} = delete $params{$column}; } } return $orig->($class,\%params); }); # Using SUPER install_sub($class,'find_ids2', sub { my $class = shift; my $super = $class->super('find_ids'); my %params = ref $_[0] ? %{ $_[0] } : (@_); foreach my $column (keys %params) { if (exists $alias_for->{$column}) { $params{$alias_for->{$column}} = delete $params{$column}; } } return $super->($class,\%params); }); # Using string eval my $sub = eval "package $class;".<<'SUB'; *find_ids3 = sub { my $class = shift; my %params = ref $_[0] ? %{ $_[0] } : (@_); foreach my $column (keys %params) { if (exists $alias_for->{$column}) { $params{$alias_for->{$column}} = delete $params{$column}; } } return $class->SUPER::find_ids(\%params); }; SUB ############### # Run the Benchmark use Benchmark qw (cmpthese); cmpthese (100000, { orig => sub {MyClass->find_ids1()}, super => sub {MyClass->find_ids2()}, eval => sub {MyClass->find_ids3()}, }); ######################### # The find_ids method in the superclass just returned #########################

Replies are listed 'Best First'.
Re^7: Using SUPER in dynamically generated subs
by ikegami (Patriarch) on Nov 13, 2006 at 20:03 UTC

    Are you benchmarking the creation of the function, its call or a likely combination of both?

    I tend to ignore benchmarks results for which I haven't seen the benchmark code. How else can their correctness and relevance be judged? Writting good benchmarks is hard.

    I usually place the benchmark code in <readmore>..</readmore> tags in the same post as the benchmark results.