#! perl -slw use strict; use Benchmark qw[ cmpthese ]; cmpthese -1, { one_eval => q[ my $t = 1; eval qq[ sub count { \$_[0] =~ tr[$t][$t] } ]; my $c = 0; $c += count( $_ ) for 1 .. 1e6; print $c; ], many_evals => q[ my $t = 1; my $c = 0; $c += eval qq[ \$_ =~ tr[$t][$t] ] for 1 .. 1e6; print $c; ], }; __END__ C:\test>junk30 600001 600001 (warning: too few iterations for a reliable count) 600001 Subroutine count redefined at (eval 2000016) line 1. 600001 Subroutine count redefined at (eval 2000017) line 1. 600001 Subroutine count redefined at (eval 2000020) line 1. 600001 (warning: too few iterations for a reliable count) s/iter many_evals one_eval many_evals 27.3 -- -98% one_eval 0.416 6451% -- #### #! perl -slw use strict; use Benchmark qw[ cmpthese ]; cmpthese -1, { one_eval => q[ my $t = 1; eval qq[ sub count { \$_[0] =~ tr[$t][$t] } ]; my $c = 0; $c += count( $_ ) for 1 .. 1e6; print $c; ], many_evals => q[ my $t = 1; my $c = 0; $c += eval qq[ \$_ =~ tr[$t][$t] ] for 1 .. 1e6; print $c; ], loop => q[ my $t = 1; my $c = 0; for my $n ( 1 .. 1e6 ) { ++$c while $n =~ m[$t]g; } print $c; ], }; __END__ C:\test>junk30 600001 600001 600001 (warning: too few iterations for a reliable count) 600001 600001 (warning: too few iterations for a reliable count) 600001 Subroutine count redefined at (eval 2000022) line 1. 600001 Subroutine count redefined at (eval 2000023) line 1. 600001 Subroutine count redefined at (eval 2000026) line 1. 600001 (warning: too few iterations for a reliable count) Rate many_evals loop one_eval many_evals 3.76e-002/s -- -97% -98% loop 1.17/s 3002% -- -52% one_eval 2.41/s 6303% 106% --