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