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

In reply to RE: utime vs. open/close by plaid
in thread utime vs. open/close by jjhorner

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.