[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / interpreter / timer.c
1 /* -*- mode: hugs-c; -*- */
2 /* --------------------------------------------------------------------------
3  * This file provides a simple mechanism for measuring elapsed time on Unix
4  * machines (more precisely, on any machine with an rusage() function).
5  * A somewhat limited version for other systems is also included, believed
6  * to be ANSI compatible, but not guaranteed ...
7  *
8  * It is included in the Hugs distribution for the purpose of benchmarking
9  * the Hugs interpreter, comparing its performance across a variety of
10  * different machines, and with other systems for similar languages.
11  *
12  * To make use of these functions, use the --enable-timer when configuring
13  * Hugs or change the setting of "WANT_TIMER" in config.h and recompile
14  * Hugs.
15  *
16  * It would be somewhat foolish to try to use the timings produced in this
17  * way for anything other than the purpose described above.  In particular,
18  * using timings to compare the performance of different versions of an
19  * algorithm is likely to give very misleading results.  The current
20  * implementation of Hugs as an interpreter, without any significant
21  * optimizations, means that there are much more significant overheads than
22  * can be accounted for by small variations in Hugs code.
23  *
24  * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
25  * All rights reserved. See NOTICE for details and conditions of use etc...
26  * Hugs version 1.4, December 1997
27  *
28  * $RCSfile: timer.c,v $
29  * $Revision: 1.2 $
30  * $Date: 1998/12/02 13:22:46 $
31  * ------------------------------------------------------------------------*/
32
33
34 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
35 #include <sys/time.h>
36 #include <sys/resource.h>
37
38 void updateTimers Args((void));
39 long millisecs  Args((long));
40 long userElapsed, systElapsed;
41
42 void updateTimers() {
43     static long lastUser = 0;
44     static long lastSyst = 0;
45     long curr;
46     struct rusage ruse;
47     getrusage(RUSAGE_SELF,&ruse);
48
49     curr        = ruse.ru_utime.tv_sec*1000000L + ruse.ru_utime.tv_usec;
50     userElapsed = curr - lastUser;
51     lastUser    = curr;
52
53     curr        = ruse.ru_stime.tv_sec*1000000L + ruse.ru_stime.tv_usec;
54     systElapsed = curr - lastSyst;
55     lastSyst    = curr;
56 }
57
58 long millisecs(t)
59 long t; {
60     return (t+500)/1000;
61 }
62 #else
63 #include <time.h>
64
65 void updateTimers Args((void));
66 long millisecs    Args((clock_t));
67 clock_t userElapsed=0, systElapsed=0;
68
69 void updateTimers() {
70     static clock_t lastUser = 0;
71     clock_t curr;
72     curr        = clock();
73     userElapsed = curr - lastUser;
74     lastUser    = curr;
75 }
76
77 long millisecs(t)
78 clock_t t; {
79     return (long)((t * 1000)/CLK_TCK);
80 }
81 #endif
82
83 /*-------------------------------------------------------------------------*/