Here is a benchmark attempt for a few variations:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
our $verbosity = 7;
our $volume = 1;
our $holy_crap = "Hmmmm.. here's the problem!";
timethese(10000000, {
'Name1' => ' v_print0( 1, "Holy Crap = holy_crap\n"); ',
'Name2' => ' v_print0( 1, "Holy Crap = $holy_crap\n"); ',
'Name3' => ' v_print1( 1, sub { "Holy Crap = holy_crap\n" } ); '
+,
'Name4' => ' v_print1( 1, sub { "Holy Crap = $holy_crap\n" } ); '
+,
'Name5' => ' v_print2( 1, \'"Holy Crap = holy_crap\n"\' ); ',
'Name6' => ' v_print2( 1, \'"Holy Crap = $holy_crap\n"\' ); ',
'Name7' => ' if ( $volume >= $verbosity ) { v_print3( "Holy Crap
+ $holy_crap\n" ) }; ',
});
And here the results:
Benchmark: timing 10000000 iterations of Name1, Name2, Name3, Name4, N
+ame5, Name6, Name7...
Name1: 12 wallclock secs (11.24 usr + 0.01 sys = 11.25 CPU) @ 88
+8888.89/s (n=10000000)
Name2: 16 wallclock secs (16.08 usr + 0.00 sys = 16.08 CPU) @ 62
+1890.55/s (n=10000000)
Name3: 14 wallclock secs (13.93 usr + -0.01 sys = 13.92 CPU) @ 71
+8390.80/s (n=10000000)
Name4: 13 wallclock secs (14.10 usr + 0.00 sys = 14.10 CPU) @ 70
+9219.86/s (n=10000000)
Name5: 11 wallclock secs (11.26 usr + 0.00 sys = 11.26 CPU) @ 88
+8099.47/s (n=10000000)
Name6: 12 wallclock secs (11.00 usr + 0.01 sys = 11.01 CPU) @ 90
+8265.21/s (n=10000000)
Name7: 1 wallclock secs ( 1.24 usr + 0.00 sys = 1.24 CPU) @ 80
+64516.13/s (n=10000000)
Conclusions seem to be, as you mentioned:
avoid calling subs in the first place, if possible
postpone string interpolation to happen only in the called sub, not before the call
The initial surprise diminished a bit after thinking about the benchmarks - but it's good to know it anyway;)
|