I'm using CGI.pm to write a CGI script and out of necessity, I have to do a lot of printing and I'm unsure of the best way to write it from a efficiency or style standpoint.

I ran a benchmark (output to the console, not to a browser) using this code:

use Benchmark qw/cmpthese/; use CGI; our $cgi = new CGI; cmpthese 5000,{ per_line => <<'EOFCODE', # Loop 10,000 times at 1 output per loop for a total of # 10,000 outputs. for (my $i = 0, $i == 10000, $i++){ print $cgi->p('This is a test.'); } EOFCODE # The following l concat => <<'EOFCODE', # Loop 1,000 times at 10 outputs per loop for a total of # 10,000 outputs. for (my $i = 0, $i == 1000, $i++){ print $cgi->p('This is a test.').$cgi->p('This is a test.'). $cgi->p('This is a test.').$cgi->p('This is a test.'). $cgi->p('This is a test.').$cgi->p('This is a test.'). $cgi->p('This is a test.').$cgi->p('This is a test.'). $cgi->p('This is a test.').$cgi->p('This is a test.'); } EOFCODE comma => <<'EOFCODE', # Loop 1,000 times at 10 outputs per loop for a total of # 10,000 outputs. for (my $i = 0, $i == 1000, $i++){ print $cgi->p('This is a test.'), $cgi->p('This is a test.'), $cgi->p('This is a test.'), $cgi->p('This is a test.'), $cgi->p('This is a test.'), $cgi->p('This is a test.'), $cgi->p('This is a test.'), $cgi->p('This is a test.'), $cgi->p('This is a test.'), $cgi->p('This is a test.'); } EOFCODE };

And got these results:

Rate concat comma per_line concat 747/s -- -0% -88% comma 749/s 0% -- -88% per_line 6098/s 716% 715% --

It would seem, from this, that printing one line at a time is clearly most efficient, even though it had to loop through 10x more times. However, I don't know if that is conclusive, or if code efficency alone should dictate the style the program is written in.

I have come for guidance: should I make each of my print statements its own line, should I join them with commas, or should I concatenate them as strings? I'm also willing to accept that it doesn't really matter at all. Thanks in advance.


In reply to Proper way to print a large number of strings by jpfarmer

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.