I threw together some bizarre benchmarking code to see how much the two methods differ on my machine. The difference was more than I expected... but I pretty much don't believe my results. Code below, once I finish explaining why one should ignore it completely. :)

The old adage is that "Premature optimization is the root of all evil." Yet another true cliche is that 80% of processing is performed in 20% of all code. Optimizing before profiling the code to find out where it's spending time can waste your life away, and lead you to sacrifice readability and maintainability where it isn't necessary to do so.

I second what dws said. I would go a step further, perhaps: write the code in the most readable and maintainable way you can. If one is going to be interpolating enough variables and subroutines that this question becomes worth thinking about, then it's time to consider using a templating system like The Template Toolkit or Text::Template. Many templating systems precompile themselves, so that they are nearly the same speed as either method.

Just for laughs, here's a code snippet that performs a rough-and-ready (read: probably meaningless) comparison of building up a string for appending versus printing a long list:

use strict; use Benchmark qw(cmpthese); use constant NUM_OF_VARS => 10000; use constant NUM_OF_TESTS => 1000; my @names = (); # Meaningless, getting rid of warning *NULL = *NULL; open(NULL, '>/dev/null') or die "Couldn't open /dev/null: $!; stopped" +; ## Initialize a bunch of variables to interpolate { no strict 'refs'; my $var_name = 'aa'; for ( 0 .. NUM_OF_VARS ) { $ {"Q::$var_name" } = "Name is '$var_name', number $_"; push(@names, "\$Q::$var_name"); $var_name++; } } # Build up two subroutine strings to eval my $list_print = "print NULL "; $list_print .= join(",\n", map( qq{'$_ is ', $_, "\\n"}, @names) ); $list_print .= ';'; my $build_print = "my \$string = '';\n"; foreach my $name (@names) { $build_print .= qq{\$string .= '$name is ';\n}; $build_print .= qq{\$string .= $name;\n}; $build_print .= qq{\$string .= "\\n";\n}; } $build_print .= "print NULL \$string;\n"; # Benchmark the subs cmpthese(NUM_OF_TESTS, { 'build_print' => $build_print, 'list_print' => $list_print, });

stephen

Update: Forgot to include the code results:

Benchmark: timing 1000 iterations of build_print, list_print... build_print: 120 wallclock secs (119.84 usr + 0.04 sys = 119.88 CPU) +@ 8.34/s (n=1000) list_print: 92 wallclock secs (91.23 usr + 0.11 sys = 91.34 CPU) @ 10 +.95/s (n=1000) Rate build_print list_print build_print 8.34/s -- -24% list_print 10.9/s 31% --

Seems in the expected direction, but I doubt the numbers themselves a great deal.


In reply to Re: Optimizing Output by stephen
in thread Optimizing Output by Dogma

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.