[project @ 2000-04-17 11:39:56 by simonmar]
[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  * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
28  * Yale Haskell Group, and the Oregon Graduate Institute of Science and
29  * Technology, 1994-1999, All rights reserved.  It is distributed as
30  * free software under the license in the file "License", which is
31  * included in the distribution.
32  *
33  * $RCSfile: timer.c,v $
34  * $Revision: 1.4 $
35  * $Date: 1999/10/15 21:41:00 $
36  * ------------------------------------------------------------------------*/
37
38
39 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
40 #include <sys/time.h>
41 #include <sys/resource.h>
42
43 void updateTimers Args((void));
44 long millisecs  Args((long));
45 long userElapsed, systElapsed;
46
47 void updateTimers() {
48     static long lastUser = 0;
49     static long lastSyst = 0;
50     long curr;
51     struct rusage ruse;
52     getrusage(RUSAGE_SELF,&ruse);
53
54     curr        = ruse.ru_utime.tv_sec*1000000L + ruse.ru_utime.tv_usec;
55     userElapsed = curr - lastUser;
56     lastUser    = curr;
57
58     curr        = ruse.ru_stime.tv_sec*1000000L + ruse.ru_stime.tv_usec;
59     systElapsed = curr - lastSyst;
60     lastSyst    = curr;
61 }
62
63 long millisecs(t)
64 long t; {
65     return (t+500)/1000;
66 }
67 #else
68 #include <time.h>
69
70 void updateTimers Args((void));
71 long millisecs    Args((clock_t));
72 clock_t userElapsed=0, systElapsed=0;
73
74 void updateTimers() {
75     static clock_t lastUser = 0;
76     clock_t curr;
77     curr        = clock();
78     userElapsed = curr - lastUser;
79     lastUser    = curr;
80 }
81
82 long millisecs(t)
83 clock_t t; {
84     return (long)((t * 1000)/CLK_TCK);
85 }
86 #endif
87
88 /*-------------------------------------------------------------------------*/