.gitignore
[prop.git] / lib-src / gc / gctimer.cc
blobd8df9522b8553d68145184dacb78dfa7fb54cb74
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are welcomed to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
21 // Allen Leung (leunga@cs.nyu.edu)
22 // 1994-1995
24 // Kudos to James Litsios <litsios@iis.ee.ethz.ch> for his tireless
25 // testing efforts and useful feedbacks. Certain part of Prop
26 // now incorporates some of James' ideas on reference counting and
27 // hash consing.
28 //////////////////////////////////////////////////////////////////////////////
30 #include <iostream.h>
31 #include <iomanip.h>
32 #include <AD/gc/gcconfig.h>
33 #include <AD/gc/gctimer.h>
35 // Use getrusage instead of times when the former is available
36 // since getrusage has higher resolution
38 #ifdef PROP_HAS_GETRUSAGE
39 # include <sys/time.h>
40 # include <sys/resource.h>
41 #else
42 # ifdef PROP_HAS_TIMES
43 # include <sys/times.h>
44 # endif
45 #endif
47 GCTimer::GCTimer() : sec(0), usec(0) {}
48 GCTimer::GCTimer(long s, long us) : sec(s), usec(us) {}
49 void GCTimer::clear() { sec = 0; usec = 0; }
51 GCTimer GCTimer::operator + (const GCTimer& b)
52 { long the_usec = (usec + b.usec) % 1000000;
53 long the_sec = sec + b.sec + (usec + usec) / 1000000;
54 return GCTimer(the_sec,the_usec);
57 GCTimer GCTimer::operator - (const GCTimer& b)
58 { long the_usec = (1000000 + usec - b.usec) % 1000000;
59 long the_sec = sec - b.sec - (usec < usec);
60 return GCTimer(the_sec,the_usec);
63 void GCTimer::operator += (const GCTimer& b)
64 { *this = *this + b; }
66 ostream& operator << (ostream& f, const GCTimer& t)
67 { int ms = t.usec / 1000;
68 f << t.sec << '.'
69 << (char)(ms/100 + '0')
70 << (char)((ms/10) % 10 + '0')
71 << (char)(ms % 10 + '0')
72 << 's';
73 return f;
76 void GCTimer::get_system_time()
78 #ifdef PROP_HAS_GETRUSAGE
79 struct rusage usage;
80 getrusage(RUSAGE_SELF,&usage);
81 usec = usage.ru_stime.tv_usec;
82 sec = usage.ru_stime.tv_sec;
83 #else
84 # ifdef PROP_HAS_TIMES
85 struct tms timing;
86 times(&timing);
87 usec = (timing.tms_stime % 60) * 1000000 / 60;
88 sec = timing.tms_stime / 60;
89 # endif
90 #endif
93 void GCTimer::get_user_time()
95 #ifdef PROP_HAS_GETRUSAGE
96 struct rusage usage;
97 getrusage(RUSAGE_SELF,&usage);
98 usec = usage.ru_utime.tv_usec;
99 sec = usage.ru_utime.tv_sec;
100 #else
101 # ifdef PROP_HAS_TIMES
102 struct tms timing;
103 times(&timing);
104 usec = (timing.tms_utime % 60) * 1000000 / 60;
105 sec = timing.tms_utime / 60;
106 # endif
107 #endif