Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: grab oldest file

by hasimir44 (Sexton)
on Jul 06, 2006 at 23:29 UTC ( #559691=note: print w/replies, xml ) Need Help??


in reply to Re: grab oldest file
in thread grab newest file

Which of these would use the least amount of resources?
$report = (sort glob "$path/*B1006*")[-1];
or
chomp($report = `ls $path/*B1006* |tail -n 1`);

Replies are listed 'Best First'.
Re^3: grab oldest file
by Tanktalus (Canon) on Jul 06, 2006 at 23:43 UTC

    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 ;-)

      That's exactly what I wanted to know. I realize that it doesn't matter at all in this example. However, I hope to someday create something where it will matter and now I at least have a better understanding. namaste
Re^3: grab newest file
by jwkrahn (Monsignor) on Jul 07, 2006 at 00:06 UTC
    If you really want to use the least resources: Updated for newest file:
    my $report = do { opendir my $dh, $path or die "open '$path' $!"; my ( $name, $mtime ) = ( '', ~0 ); while ( my $file = readdir $dh ) { stat "$path/$file"; ( $name, $mtime ) = ( $file, -M _ ) if $file =~ /B1006/ and $m +time > -M _; } $name; };
      Comparing mtime (timestamp) and -M is bogus. -M gives you the script start time minus file modification time, in days.

      update - oops, sometimes I'm a lousy reader - the referred post compares only -M against -M. Read the above just as a general statement ;-)

      thanks jwkrahn

      --shmem

      _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                    /\_/(q    /
      ----------------------------  \__(m.====.(_("always off the crowd"))."
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        The code I posted is only comparing the -M value with other -M values. The $mtime in the code is just a variable name.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://559691]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2023-05-29 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?