CoCalc Public Fileswww / cgi-bin / openwebmail / misc / test / gettimeofday.plOpen with one click!
Author: William A. Stein
1
# this is used for profiling
2
use strict;
3
4
use vars qw(%lasttimeofday);
5
%lasttimeofday=();
6
7
sub gettimeofday {
8
my ($SYS_gettimeofday, $timeval, $timezone, $sec, $usec);
9
10
$SYS_gettimeofday = 116; # should really be from sys/syscalls.ph
11
$timeval = $timezone = ("\0" x 4) x 2;
12
syscall($SYS_gettimeofday, $timeval, $timezone)
13
&& die "gettimeofday failed: $!";
14
($sec, $usec) = unpack("L2", $timeval);
15
return $sec + $usec/1e6;
16
}
17
18
sub timeofday_init {
19
%lasttimeofday=();
20
}
21
22
sub timeofday_diff {
23
my $tag=$_[0]||'default';
24
25
my $t=$lasttimeofday{$tag};
26
$lasttimeofday{$tag}=gettimeofday();
27
if ($t) {
28
return(sprintf("%10.6f", $lasttimeofday{$tag}-$t));
29
} else {
30
return(sprintf("%10.6f", 0));
31
}
32
}
33
34
1;
35