Not related to your question, but my pet peeve is code that proceesses data element by element, when it could be handled all at once.

There's no need to process your output line by line. If you print an array on it's own:

print PROG @array

the array elements are printed without any separator. If this is a CGI script, separators are ignored by the browser, anyway, so that should be the fastest solution. But, for some odd reason, it is actually quite slow.

If it isn't HTML, and it isn't convenient to add newline characters when the array is generated, you'll need to introduce the newlines at the print. If you have an array in a double-quoted strring, the array elements are printed separated by the value of the variable $". So temporarily re-defined $" to be newline, and use the array in a string ... the string will need a newline at the end.

{ local $" = "\n"; print PROG "@array\n"; }

That's equivalent to using join to convert the array into a string, and equally fast.

print PROG join( "\n", @array), "\n";

That makes me think the underlying code is very much identical. Don't forget that Perl built-ins ( and natively coded module routines ) are fast, while re-implementing equivalent constructs in perl is slower.

I timed the options, one hundred iterations of each with an array of one million elements. join and local are 6 times as fast as manual looping and 3 times as fast as printing the array outside a string.

use Benchmark; for ( 1..1000000 ) { push @a, "$i"; } open PROG, ">/dev/nul"; timethese( 100], { manual => sub {for $line ( @a ){ print PROG "$line\n";}}, join => sub {print PROG join( "\n", @a ), "\n" }, local => sub {local $"="\n"; print PROG "@a\n";} html => sub {print PROG @a;} }); ##### output -> $ perl t.pl Benchmark: timing 100 iterations of join, local, manual... html:133 wallclock secs (131.79 usr+ 0.15 sys = 131.94 CPU) @ 0.76 +/s (n=100) join: 45 wallclock secs (44.04 usr + 0.01 sys = 44.05 CPU) @ 2.27/ +s (n=100) local: 45 wallclock secs (44.51 usr + 0.00 sys = 44.51 CPU) @ 2.25/ +s (n=100) manual:275 wallclock secs (274.17 usr+ 0.04 sys = 274.21 CPU) @ 0.36 +/s (n=100)

--
TTTATCGGTCGTTATATAGATGTTTGCA


In reply to Re: pipes and return data by TomDLux
in thread pipes and return data by Anonymous Monk

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.