ASL  0.1.6
Advanced Simulation Library
aslTimer.h
Go to the documentation of this file.
1 /*
2  * Advanced Simulation Library <http://asl.org.il>
3  *
4  * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5  *
6  *
7  * This file is part of Advanced Simulation Library (ASL).
8  *
9  * ASL is free software: you can redistribute it and/or modify it
10  * under the terms of the GNU Affero General Public License as
11  * published by the Free Software Foundation, version 3 of the License.
12  *
13  * ASL is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 
24 #ifndef ASLTIMER_H
25 #define ASLTIMER_H
26 
27 #include <sys/time.h>
28 
29 namespace asl{
30 
32  class Timer {
33  private:
34  clock_t _c;
35  double _t;
36  inline double tod(){
37  timeval tim;
38  gettimeofday(&tim,NULL);
39  return tim.tv_sec+(tim.tv_usec/1000000.0);
40  }
41  public:
42  inline Timer():_c(0),_t(0){};
43  inline void start(){_c=-clock();_t=-tod();}
44  inline void resume(){_c-=clock();_t-=tod();}
45  inline void stop(){_c+=clock();_t+=tod();}
46  inline const double getTime() const{return _t;}
47  inline const double getClockTime() const{return (float)_c/CLOCKS_PER_SEC;}
48  inline const double getProcessorLoad() const{return getClockTime()/getTime();}
49  inline void reset(){_c=0; _t=0;}
50  inline const double getExpectedTime(double fractTime){return _t/fractTime;}
51  inline const double getLeftTime(double fractTime){return (1.-fractTime)*getExpectedTime(fractTime);}
52  };
53 
54  // waits \p dt seconds, uses loop
55  inline void delay(double dt)
56  {
57  timeval t;
58  gettimeofday(&t,NULL);
59  double t0(t.tv_sec+(t.tv_usec/1000000.0));
60  double tc(t0);
61  while (t0+dt>tc)
62  {
63  gettimeofday(&t,NULL);
64  tc=t.tv_sec+(t.tv_usec/1000000.0);
65  }
66  }
67 
68 } //namespace acl
69 
70 #endif // aslGeneratorS_H
const double getTime() const
Definition: aslTimer.h:46
const double getExpectedTime(double fractTime)
Definition: aslTimer.h:50
Advanced Simulation Library.
Definition: aslDataInc.h:30
const double getProcessorLoad() const
Definition: aslTimer.h:48
void resume()
Definition: aslTimer.h:44
const double getClockTime() const
Definition: aslTimer.h:47
void reset()
Definition: aslTimer.h:49
const double getLeftTime(double fractTime)
Definition: aslTimer.h:51
void delay(double dt)
Definition: aslTimer.h:55
void stop()
Definition: aslTimer.h:45
void start()
Definition: aslTimer.h:43