http://www.perlmonks.org?node_id=17418


in reply to Apache Timeout module

First thing, the way you have it set up to check timestamps, the line if ($time_to_die >= ($current_time - $last_time)) { seems to suggest that $time_to_die is in seconds, as both $current_time and $last_time are in seconds, but I believe you were expecting $time_to_die in minutes, so you may want to throw a *= 60 in there or something.

One thing I was considering about this is that there isn't really a need to throw the timestamp into a file, since basically you're just storing the exact same number as is already contained in the file's modification time. Since you're already effectively performing a stat via the -e test, you can then just use the cached filehandle _ to obtain the modification time and cut out an additional file opening/reading/writing.

use Benchmark; use strict; my $time_to_die = 15; my $time_file_1 = "time1.txt"; my $time_file_2 = "time2.txt"; my $current_time = time(); # kept this outside the benchmark timethese(100000, { 'Time In File' => \&timeinfile, 'File Mod Time' => \&filemodtime }); sub timeinfile { if (-e $time_file_1) { open (TIME, "$time_file_1") || warn "Can't open Time file: $!"; my $last_time = <TIME> || warn "Unable to get timestamp from fil +e: $!"; close TIME; if ($time_to_die >= ($current_time - $last_time)) { open (TIME, ">$time_file_1"); print TIME $current_time || warn "Unable to write timestamp t +o file: $!"; close TIME; } else { unlink($time_file_1) or warn "Can't unlink file: $!"; } } else { open (TIME, ">$time_file_1"); print TIME $current_time; close TIME; } } sub filemodtime { if(-e $time_file_2) { my $last_time = (stat(_))[9]; if($time_to_die >= ($current_time - $last_time)) { open (TIME, ">$time_file_2"); close TIME; } else { unlink($time_file_2) or warn "Can't unlink file: $!"; } } else { open (TIME, ">$time_file_2"); close TIME; } } Benchmark: timing 100000 iterations of File Mod Time, Time In File... File Mod Time: 11 wallclock secs ( 6.97 usr + 3.21 sys = 10.18 CPU) Time In File: 48 wallclock secs (21.26 usr + 25.77 sys = 47.03 CPU)