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" ) }; ', });
sub v_print0 { my $volume = shift; my $message = shift; if ( $volume >= $verbosity ) { print $message; } } sub v_print1 { my $volume = shift; my $code = shift; if ( $volume >= $verbosity ) { print &$code; } } sub v_print2 { my $volume = shift; my $code = shift; if ( $volume >= $verbosity ) { print eval( $code ); } } sub v_print3 { my $message = shift; print $message; }
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;)

    In reply to Re^2: Macro in perl code? by Krambambuli
    in thread Macro in perl code? by pileofrogs

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.