vul_timer.cxx
Go to the documentation of this file.
1 // This is core/vul/vul_timer.cxx
2 #include <ctime>
3 #include <iostream>
4 #include "vul_timer.h"
5 //:
6 // \file
7 //
8 // Copyright (C) 1991 Texas Instruments Incorporated.
9 //
10 // Permission is granted to any individual or institution to use, copy, modify,
11 // and distribute this software, provided that this complete copyright and
12 // permission notice is maintained, intact, in all copies and supporting
13 // documentation.
14 //
15 // Texas Instruments Incorporated provides this software "as is" without
16 // express or implied warranty.
17 //
18 // Created: BMK 07/14/89 Initial design and implementation
19 // Updated: LGO 09/23/89 Conform to COOL coding style
20 // Updated: AFM 12/31/89 OS/2 port
21 // Updated: DLS 03/22/91 New lite version
22 // Updated: VDN 10/14/93 ANSI C does not have user/system time.
23 //
24 // The Timer class provides timing code for performance evaluation. This code
25 // was originally written by Joe Rahmeh at UT Austin.
26 //
27 // User time:
28 // time cpu spends in user mode on behalf of the program.
29 // System time:
30 // time cpu spends in system mode on behalf of the program.
31 // Real time:
32 // what you get from a stop watch timer.
33 //
34 
35 #ifdef _MSC_VER
36 # include <vcl_msvc_warnings.h>
37 #endif
38 #include <vcl_sys/time.h>
39 # undef __USE_BSD
40 
42 {
43 #if !defined(_WIN32) || defined(__CYGWIN__)
44  tms usage0; // usage mark.
45  struct timeval real0; // wall clock mark.
46 #else
47  std::clock_t usage0;
48  struct _timeb real0;
49 #endif
50 };
51 
52 // for CLK_TCK
53 
54 
55 //#define CLK_TCK _sysconf(3) in <climits> has error
56 
57 #if defined(_WIN32) && !defined(__CYGWIN__)
58 #include <direct.h> // for sysconf()
59 #else
60 #include <unistd.h>
61 #endif
62 #undef CLK_TCK
63 #define CLK_TCK sysconf(_SC_CLK_TCK)
64 
66  : data(new vul_timer_data)
67 {
68  mark();
69 }
70 
72 {
73  delete data;
74  data = nullptr;
75 }
76 
77 //: Sets the reference time to now.
78 
80 {
81 #if !defined(_WIN32) || defined(__CYGWIN__)
82  times(&data->usage0); // user/system time
83 #ifndef SYSV
84  struct timezone tz;
85  gettimeofday(&data->real0, &tz); // wall clock time
86 #else
87 #if VXL_TWO_ARG_GETTIME
88  gettimeofday(&data->real0, (struct timezone*)0);
89 #else
90  gettimeofday(&data->real0);
91 #endif
92 #endif
93 #else
94  // Win32 section
95  data->usage0 = std::clock();
96  _ftime(&data->real0);
97 #endif
98 }
99 
100 //: Returns the number of milliseconds of wall clock time, since last mark().
101 
103 {
104  long s;
105 
106 #if !defined(_WIN32) || defined(__CYGWIN__)
107  struct timeval real_time; // new real time
108 #ifndef SYSV
109  struct timezone tz;
110  gettimeofday(&real_time, &tz); // wall clock time
111 #else
112 #if VXL_TWO_ARG_GETTIME
113  gettimeofday(&real_time, (struct timezone*)0);
114 #else
115  gettimeofday(&real_time);
116 #endif
117 #endif
118  s = real_time.tv_sec - data->real0.tv_sec;
119  long us = real_time.tv_usec - data->real0.tv_usec;
120 
121  if (us < 0) { us += 1000000; --s; }
122  return long(1000.0*s + us / 1000.0 + 0.5);
123 
124 #else
125  // Win32 section
126  struct _timeb real_time;
127  _ftime(&real_time);
128  s = long(real_time.time - data->real0.time);
129  long ms = real_time.millitm - data->real0.millitm;
130 
131  if (ms < 0) { ms += 1000; --s; }
132  return 1000*s + ms;
133 #endif
134 }
135 
136 
138 {
139 #if !defined(_WIN32) || defined(__CYGWIN__)
140  tms usage;
141  times(&usage); // new user/system time
142  return (usage.tms_utime - data->usage0.tms_utime) * 1000 / CLK_TCK;
143 #else
144  std::clock_t usage = std::clock();
145  return (usage - data->usage0) / (CLOCKS_PER_SEC/1000);
146 #endif
147 }
148 
149 //: Returns the number of milliseconds spent in user-process or operating system respectively, since last mark().
150 
152 {
153 #if !defined(_WIN32) || defined(__CYGWIN__)
154  tms usage;
155  times(&usage); // new user/system time
156  return (usage.tms_stime - data->usage0.tms_stime) * 1000 / CLK_TCK;
157 #else
158  return 0L;
159 #endif
160 }
161 
162 // Returns the number of milliseconds spent in user-process AND
163 // operating system, since last mark().
164 
166 {
167 #if !defined(_WIN32) || defined(__CYGWIN__)
168  tms usage;
169  times(&usage); // new user/system time
170  return (usage.tms_utime + usage.tms_stime -
171  data->usage0.tms_utime - data->usage0.tms_stime) * 1000 / CLK_TCK;
172 #else
173  std::clock_t usage = std::clock();
174  return (usage - data->usage0) / (CLOCKS_PER_SEC/1000);
175 #endif
176 }
177 
178 //: Display user and real time since the last mark.
179 void vul_timer::print(std::ostream& s)
180 {
181  s << "Time: user " << user() / 1000.0 << ", real " << this->real() / 1000.0 << std::endl;
182 }
vul_timer()
construct and reset counter to now.
Definition: vul_timer.cxx:65
vul_timer_data * data
struct containing timer data.
Definition: vul_timer.h:63
long system()
System time (ms) since last mark.
Definition: vul_timer.cxx:151
void print(std::ostream &s)
Display user and real time since the last mark.
Definition: vul_timer.cxx:179
long user()
User time (ms) since last mark.
Definition: vul_timer.cxx:137
void mark()
Reset the counted to now.
Definition: vul_timer.cxx:79
long all()
User+system time (ms) since last mark.
Definition: vul_timer.cxx:165
A timing facility for C++.
long real()
Real time (ms) since last mark.
Definition: vul_timer.cxx:102
struct timeval real0
Definition: vul_timer.cxx:45
#define CLK_TCK
Definition: vul_timer.cxx:63