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-2026 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25#include <assert.h>
26
27#ifdef _WIN32
28#include <windows.h>
29#else
30#include <sys/times.h>
31#include <sys/time.h>
32#endif
33#include <time.h>
34
35#include "soplex/spxdefines.h"
37
38namespace soplex
39{
40
41// start timer, resume accounting user, system and real time.
43{
44 // ignore start request if timer is running
45 if(status != RUNNING)
46 {
47#ifdef _WIN32
48 sec = -::time(nullptr);
49#else
50 struct timeval tp; /*lint !e86*/
51
52 gettimeofday(&tp, nullptr);
53
54 if(tp.tv_usec > usec) /*lint !e115 !e40*/
55 {
56 sec = -(tp.tv_sec + 1); /*lint !e115 !e40*/
57 usec = (1000000 - tp.tv_usec); /*lint !e115 !e40*/
58 }
59 else
60 {
61 sec = -tp.tv_sec; /*lint !e115 !e40*/
62 usec = -tp.tv_usec; /*lint !e115 !e40*/
63 }
64
65#endif
66
68 }
69
70 lasttime = 0.0;
71}
72
73// stop timer, return accounted wallclock time.
75{
76 // status remains unchanged if timer is not running
77 if(status == RUNNING)
78 {
79#ifdef _WIN32
80 // we need the blank specifier to distiguish this method from WallclockTimer::time
81 sec += ::time(nullptr);
82#else
83 struct timeval tp; /*lint !e86*/
84
85 gettimeofday(&tp, nullptr);
86
87 if(tp.tv_usec + usec > 1000000) /*lint !e115 !e40*/
88 {
89 sec += (tp.tv_sec + 1); /*lint !e115 !e40*/
90 usec -= (1000000 - tp.tv_usec); /*lint !e115 !e40*/
91 }
92 else
93 {
94 sec += tp.tv_sec; /*lint !e115 !e40*/
95 usec += tp.tv_usec; /*lint !e115 !e40*/
96 }
97
98#endif
99
100 status = STOPPED;
102 }
103
104 return lasttime;
105}
106
107
109{
110 // only update times if timer is still running
111 if(status == RUNNING)
112 {
113#ifdef _WIN32
114 // we need the blank specifier to distiguish this method from WallclockTimer::time
115 lasttime = wall2sec(sec + ::time(nullptr), 0);
116#else
117 struct timeval tp; /*lint !e86*/
118
119 gettimeofday(&tp, nullptr);
120
121 // check whether the microseconds add up to more than a second
122 if(tp.tv_usec + usec > 1000000) /*lint !e115 !e40*/
123 lasttime = wall2sec(sec + tp.tv_sec + 1, /*lint !e115 !e40*/
124 (usec - 1000000) + tp.tv_usec); /*lint !e115 !e40*/
125 else
126 lasttime = wall2sec(sec + tp.tv_sec, /*lint !e115 !e40*/
127 usec + tp.tv_usec); /*lint !e115 !e40*/
128
129#endif
130 }
131
132 return lasttime;
133}
134
136{
137 return lasttime;
138}
139
140} // namespace soplex
@ RUNNING
running
Definition: timer.h:97
@ STOPPED
stopped
Definition: timer.h:96
enum soplex::Timer::@19 status
status of the timer
virtual Real stop()
stop timer, return accounted user time.
virtual Real lastTime() const
virtual void start()
start timer, resume accounting user, system and real time.
virtual Real time() const
Real wall2sec(time_t s, time_t us) const
convert wallclock time to secounds.
time_t usec
microseconds
Everything should be within this namespace.
double Real
Definition: spxdefines.h:269
Debugging, floating point type and parameter definitions.
WallclockTimer class.