OK, since I am feeling kind of curious today, I decided to try benchmarking the various approaches. On my first run (with 100 iterations), I got:
Method 1 is foreach my $line (`top -b -n 1`) and creating the hash keys as we go.
Method 2 is foreach my $line (`top -b -n 1`) and precreating the hash keys.
Method 3 is foreach my $line (@raw) where @raw=`top -b -n 1`and precreating the hash keys.
Method 4 is while (my $line = <TOP>) with open(TOP, "top -b -n 1|") and precreating the hash keys.
Method 1 mean:  0.560507924556732 stddev:  0.00320759260168446!
Method 2 mean:  0.560737266540527 stddev:  0.00290542841782241!
Method 3 mean:  0.561151208877563 stddev:  0.00359781593804784!
Method 4 mean:  0.558518676757812 stddev:  0.00518282488688389!
T-stat for mean 1 and mean 2 is 0.529922998231174
T-stat for mean 1 and mean 3 is 1.33459955560607
T-stat for mean 1 and mean 4 is 3.26368009466516
T-stat for mean 2 and mean 3 is 0.895111546983644
T-stat for mean 2 and mean 4 is 3.73396330152602
T-stat for mean 3 and mean 4 is 4.17253188415264

Note that I did not throw away outliers, and I have no control over what other users were doing on the machine, so this is by no means rigorous, yet I think it's still useful. I think it's pretty clear that piped opens beat the other methods hands down--good call, McDarren!

Anyway, this led me to try another combination with the same methodology and under the same conditions, the results of which follow:
Method 1 is while (my $line = <TOP>) with open(TOP, "top -b -n 1|") and creating the hash keys as we go.
Method 2 is while (my $line = <TOP>) with open(TOP, "top -b -n 1|") and precreating the hash keys.
Method 1 mean:  0.557748141288757 stddev:  0.00368823257386979!
Method 2 mean:  0.558288831710816 stddev:  0.00673477747628604!
T-stat for mean 1 and mean 2 is 0.704155995405561
A t-stat of 0.7 for r=100 gives you a confidence of between 75 and 90%; that's not bad. It might be just as much an artifact of the escape clause as anything else, but it's interesting.

Code available upon request.

Thanks again,
T.

I like computer programming because it's like Legos for the mind.

In reply to Re: How best to tell when my hash is "full" (all values defined)? by OfficeLinebacker
in thread How best to tell when my hash is "full" (all values defined)? by OfficeLinebacker

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.