diff options
Diffstat (limited to 'src/common/time.c')
-rw-r--r-- | src/common/time.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/common/time.c b/src/common/time.c new file mode 100644 index 0000000..84d8fe1 --- /dev/null +++ b/src/common/time.c @@ -0,0 +1,34 @@ +#include "../h/gsupport.h" + +/* + * millisec - returns execution time in milliseconds. Time is measured + * from the function's first call. The granularity of the time is + * generally larger than one millisecond and on some systems it may + * only be accurate to the second. + * + * For some unfathomable reason, the Open Group's "Single Unix Specification" + * requires that the ANSI C clock() function be defined in units of 1/1000000 + * second. This means that the result overflows a 32-bit signed clock_t + * value only about 35 minutes. Consequently, we use the POSIX standard + * times() function instead. + */ + +long millisec() + { + static long clockres = 0; + static long starttime = 0; + long curtime; + struct tms tp; + + times(&tp); + curtime = tp.tms_utime + tp.tms_stime; + if (clockres == 0) { + #ifdef CLK_TCK + clockres = CLK_TCK; + #else + clockres = sysconf(_SC_CLK_TCK); + #endif + starttime = curtime; + } + return (long) ((1000.0 / clockres) * (curtime - starttime)); + } |