I was looking at maintaining activity time by checking the last access time of a file, and when a user was active, it would update the access time of a file.

To try to streamline my code, I wanted to see if 'utime' was faster than 'open/close', so I wrote the following benchmark:

#!/usr/bin/perl -w use warnings; use Benchmark; my $current_time = time(); my $filename = "test.txt"; sub file_open { open (FILE, "$filename"); close FILE; } sub stat_change { utime($current_time, (stat($filename))[9], $filename); } timethese(3000000, {'openfile' => file_open(), 'changestat' => stat_ch +ange()});

No matter how many iterations I tried, it always returned really low results:

[11:08:40 jhorner@gateway scripts]$ ./20000619-1.pl Benchmark: timing 3000000 iterations of changestat, openfile... changestat: 0 wallclock secs ( 0.40 usr + 0.00 sys = 0.40 CPU) @ 75 +00000.00/s (n=3000000) (warning: too few iterations for a reliable count) openfile: 0 wallclock secs (-0.33 usr + 0.00 sys = -0.33 CPU) @ -9 +090909.09/s (n=3000000) (warning: too few iterations for a reliable count)

So, I decided to add a stumbling block to both subroutines:

#!/usr/bin/perl -w use warnings; use Benchmark; my $current_time = time(); my $filename = "test.txt"; sub file_open { open (FILE, "$filename"); close FILE; my $i; $i++; } sub stat_change { utime($current_time, (stat($filename))[9], $filename); my $i; $i++; } timethese(3000000, {'openfile' => file_open(), 'changestat' => stat_ch +ange()});

The results returned much better, but still not really promising:

[11:10:32 jhorner@gateway scripts]$ ./20000619-1.pl Benchmark: timing 3000000 iterations of changestat, openfile... changestat: 1 wallclock secs ( 0.88 usr + 0.00 sys = 0.88 CPU) @ 34 +09090.91/s (n=3000000) openfile: 2 wallclock secs ( 0.91 usr + 0.00 sys = 0.91 CPU) @ 32 +96703.30/s (n=3000000)

While the benchmark told me that it only took 1 wallclock sec, it really took 10 or 15 seconds.

So, my meditation is this:

Should there be a better method of benchmarking this type
of test, or should I find a better way to benchmark?

The test, while far from infallible, should give reasonable
results.  It doesn't however.  Is 'utime' better than 'open/close'
or vice versa?  Is this all a method of system performance on I/O?

Is this question even worth asking?
jj

In reply to 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.