The first one. However, the reason for "a)" above was not about resources used, it's more about resources available. What is the responsiveness required by your code, and is it meeting that responsiveness requirement?

One thing about premature optimisation is that many programmers make the mistake of thinking that saving CPU time is important in and of itself. It isn't - if you don't use the CPU time, the CPU probably will sit idle instead. The delta cost in electricity probably isn't going to be noticed, and surely will be less than paying for your time or electricity in posting your question.

If, however, your script computer is going through this particular piece of code thousands of times per second, where the difference of this overhead is important, well, that becomes another matter. For example, in a heavily-used web server (where "heavily-used" is entirely dependant on the web server and the overall CPU intensity of the CGI code).

Mind you, if your question was "I'm trying to get this to work on a Windows box and I'm trying to avoid requiring everyone to install the GNU tools", then that would be a very good answer to my question "a" above. In that case, it would no longer work ;-)

As to why the first one uses less resources: the second one launches three processes, and sets up two pipes. The first one does everything in the current process. Even on unix/linux where fork overhead is relatively small, creating processes still is a fairly large overhead (e.g., doing lots of stuff to the process tables in the kernel, lots of memory management, etc.). The processes? Obviously, /bin/ls and /bin/tail (or wherever they are). But also /bin/sh. And perl creates a pipe from sh to itself, while sh creates a pipe from ls to tail. I'm not sure how much overhead there is in creating pipes - but it has to be more than not creating pipes ;-)


In reply to Re^3: grab oldest file by Tanktalus
in thread grab newest file by hasimir44

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.