Scippy

SoPlex

Sequential object-oriented simPlex

wallclocktimer.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 #include <assert.h>
17 
18 #if defined(_WIN32) || defined(_WIN64)
19 #include <windows.h>
20 #else
21 #include <sys/times.h>
22 #include <sys/time.h>
23 #endif
24 #include <time.h>
25 
26 #include "soplex/spxdefines.h"
27 #include "soplex/wallclocktimer.h"
28 
29 namespace soplex
30 {
31 
32 // start timer, resume accounting user, system and real time.
34 {
35  // ignore start request if timer is running
36  if(status != RUNNING)
37  {
38 #if !defined(_WIN32) && !defined(_WIN64)
39  struct timeval tp; /*lint !e86*/
40 #endif
41 #if defined(_WIN32) || defined(_WIN64)
42  sec = -::time(NULL);
43 #else
44  gettimeofday(&tp, NULL);
45 
46  if(tp.tv_usec > usec) /*lint !e115 !e40*/
47  {
48  sec = -(tp.tv_sec + 1); /*lint !e115 !e40*/
49  usec = (1000000 - tp.tv_usec); /*lint !e115 !e40*/
50  }
51  else
52  {
53  sec = -tp.tv_sec; /*lint !e115 !e40*/
54  usec = -tp.tv_usec; /*lint !e115 !e40*/
55  }
56 
57 #endif
58  status = RUNNING;
59  }
60 
61  lasttime = 0.0;
62 }
63 
64 // stop timer, return accounted wallclock time.
66 {
67  // status remains unchanged if timer is not running
68  if(status == RUNNING)
69  {
70 #if !defined(_WIN32) && !defined(_WIN64)
71  struct timeval tp; /*lint !e86*/
72 #endif
73 
74 #if defined(_WIN32) || defined(_WIN64)
75  // we need the blank specifier to distiguish this method from WallclockTimer::time
76  sec += ::time(NULL);
77 #else
78  gettimeofday(&tp, NULL);
79 
80  if(tp.tv_usec + usec > 1000000) /*lint !e115 !e40*/
81  {
82  sec += (tp.tv_sec + 1); /*lint !e115 !e40*/
83  usec -= (1000000 - tp.tv_usec); /*lint !e115 !e40*/
84  }
85  else
86  {
87  sec += tp.tv_sec; /*lint !e115 !e40*/
88  usec += tp.tv_usec; /*lint !e115 !e40*/
89  }
90 
91 #endif
92  status = STOPPED;
93  lasttime = wall2sec(sec, usec);
94  }
95 
96  return lasttime;
97 }
98 
99 
101 {
102 #if !defined(_WIN32) && !defined(_WIN64)
103  struct timeval tp; /*lint !e86*/
104 #endif
105 
106  // only update times if timer is still running
107  if(status == RUNNING)
108  {
109 #if defined(_WIN32) || defined(_WIN64)
110  // we need the blank specifier to distiguish this method from WallclockTimer::time
111  lasttime = wall2sec(sec + ::time(NULL), 0);
112 #else
113  gettimeofday(&tp, NULL);
114 
115  // check whether the microseconds add up to more than a second
116  if(tp.tv_usec + usec > 1000000) /*lint !e115 !e40*/
117  lasttime = wall2sec(sec + tp.tv_sec + 1, /*lint !e115 !e40*/
118  (usec - 1000000) + tp.tv_usec); /*lint !e115 !e40*/
119  else
120  lasttime = wall2sec(sec + tp.tv_sec, /*lint !e115 !e40*/
121  usec + tp.tv_usec); /*lint !e115 !e40*/
122 
123 #endif
124  }
125 
126  return lasttime;
127 }
128 
130 {
131  return lasttime;
132 }
133 
134 } // namespace soplex
virtual Real time() const
virtual Real lastTime() const
WallclockTimer class.
time_t usec
microseconds
Real wall2sec(time_t s, time_t us) const
convert wallclock time to secounds.
double Real
Definition: spxdefines.h:218
virtual void start()
start timer, resume accounting user, system and real time.
Debugging, floating point type and parameter definitions.
Everything should be within this namespace.
enum soplex::Timer::@19 status
status of the timer
virtual Real stop()
stop timer, return accounted user time.