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