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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: utime vs. open/close
by plaid (Chaplain) on Jun 20, 2000 at 00:28 UTC | |
by jjhorner (Hermit) on Jun 20, 2000 at 04:00 UTC | |
|
RE: utime vs. open/close
by Odud (Pilgrim) on Jun 19, 2000 at 22:59 UTC |