in reply to Re^2: grab oldest file
in thread grab newest file
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 ;-)