http://www.perlmonks.org?node_id=800530


in reply to Re: Formatting dynamic method names
in thread Formatting dynamic method names

Interesting - here are the results:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); package foo; { sub set_var1 { } sub set_var2 { } sub set_var3 { } sub set_var4 { } sub set_var5 { } } package main; my $object = bless {}, 'foo'; my %hash = map { ( "var$_" => $_ ) } ( 1 .. 5 ); sub temp_var { for ( keys %hash ) { my $method = "set_$_"; $object->$method( $hash{$_} ); } } sub reused_lexical { my $method; for ( keys %hash ) { $method = "set_$_"; $object->$method( $hash{$_} ); } } sub deref { for ( keys %hash ) { $object ->${ \"set_$_" }( $hash{$_} ); } } cmpthese( 1_000_000, { temp_var => \&temp_var, deref => \&deref, reused_lexical => \&reused_lexical, } );
Rate deref temp_var reused_lexic +al deref 173010/s -- -21% -2 +7% temp_var 218818/s 26% -- - +8% reused_lexical 238095/s 38% 9% +--

(Note: these benchmarks are for interest only, and are firmly in the field of premature optimization)