Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

RE: utime vs. open/close

by plaid (Chaplain)
on Jun 20, 2000 at 00:28 UTC ( #18869=note: print w/replies, xml ) Need Help??


in reply to utime vs. open/close

Several comments about your code.

First, it would probably be better to put the line my $current_time = time(); inside the stat_change sub, as the getting of the current time isn't something that both subs need, so can't fairly be factored out.

Secondly, in the file_open sub, you are opening the file for reading, which won't create it if it's not there, and if it is there it won't update the modification time.

Third, I don't really see a reason for that stat call in stat_change, as setting the modification time to $current_time as well probably won't have any reason to keep the modification time the same, if I remember your program correctly.

Fourth, and this is the major problem with the code, you're not passing the subroutine arguments to timethese correctly. You need to pass them by reference, but what you're doing is actually calling them and passing the return values to timethese.

My code:

#!/usr/bin/perl -w use Benchmark; my $filename = "test.txt"; sub file_open { open (FILE, ">$filename"); close FILE; } sub utime1 { my $current_time = time(); utime($current_time, (stat($filename))[9], $filename); } sub utime2 { my $current_time = time(); utime($current_time, $current_time, $filename); } timethese(500000, { 'openfile' => \&file_open, 'utime1' => \&utime1, 'utime2' => \&utime2 }); Benchmark: timing 500000 iterations of openfile, utime1, utime2... openfile: 32 wallclock secs (19.46 usr + 11.91 sys = 31.37 CPU) utime1: 18 wallclock secs (11.93 usr + 5.75 sys = 17.68 CPU) utime2: 7 wallclock secs ( 3.92 usr + 3.03 sys = 6.95 CPU)
Note: Since I'm the one who gave you the open/close idea in the first place, I just thought I'd say that if I had remembered utime when I was making that post, I would definitely have suggested that instead:)

Replies are listed 'Best First'.
RE: RE: utime vs. open/close
by jjhorner (Hermit) on Jun 20, 2000 at 04:00 UTC

    Good response, but I have a few counter points:

    First point: Didn't think of that.

    Second point: The file is there. I don't want creating the file to be part of the issue.

    Third point: I'm working on a different version for work where I need both file access time and file modification time.

    Fourth point: I didn't really consider that. Oops. I figured there was something wrong somewhere.

    Your utime vs openfile comparison is probably what I need most. Since I need to find the best way to update access time only, I need to open read-only and just update access time.

    Thanks for the response.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-05-25 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (145 votes). Check out past polls.

    Notices?