SOSpin is hosted by Hepforge, IPPP Durham
SOSpin  1.0.0
timer.cpp
Go to the documentation of this file.
1 // Timer.cpp
3 // =========
4 // High Resolution Timer.
5 // This timer is able to measure the elapsed time with 1 micro-second accuracy
6 // in both Windows, Linux and Unix system
7 //
8 // AUTHOR: Song Ho Ahn (song.ahn@gmail.com)
9 // CREATED: 2003-01-13
10 // UPDATED: 2006-01-13
11 //
12 // Copyright (c) 2003 Song Ho Ahn
14 
20 #include "timer.h"
21 #include <stdlib.h>
22 
23 
24 
25 
27 const std::string currentDateTime() {
28  time_t now = time(0);
29  struct tm tstruct;
30  char buf[80];
31  tstruct = *localtime(&now);
32  strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
33 
34  return buf;
35 }
36 
37 
38 
41 #ifdef WIN32
42  QueryPerformanceFrequency(&frequency);
43  startCount.QuadPart = 0;
44  endCount.QuadPart = 0;
45 #else
46  startCount.tv_sec = startCount.tv_usec = 0;
47  endCount.tv_sec = endCount.tv_usec = 0;
48 #endif
49 
50  stopped = 0;
53 }
54 
57 }
58 
59 
60 
62 void Timer::start(){
63  stopped = 0; // reset stop flag
64 #ifdef WIN32
65  QueryPerformanceCounter(&startCount);
66 #else
67  gettimeofday(&startCount, NULL);
68 #endif
69 }
70 
71 
72 
74 void Timer::stop(){
75  stopped = 1; // set timer stopped flag
76 
77 #ifdef WIN32
78  QueryPerformanceCounter(&endCount);
79 #else
80  gettimeofday(&endCount, NULL);
81 #endif
82 }
83 
84 
85 
86 
89 #ifdef WIN32
90  if(!stopped)
91  QueryPerformanceCounter(&endCount);
92 
93  startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
94  endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
95 #else
96  if(!stopped)
97  gettimeofday(&endCount, NULL);
98 
99  startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
100  endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
101 #endif
102 
104 }
105 
106 
107 
110  return this->getElapsedTimeInMicroSec() /1000;
111 }
112 
113 
114 
117  return this->getElapsedTimeInMicroSec() * 0.000001;
118 }
119 
120 
121 
124  return this->getElapsedTimeInSec();
125 }
double getElapsedTimeInSec()
Get elapsed time in second (same as getElapsedTime)
Definition: timer.cpp:116
timeval endCount
Definition: timer.h:85
~Timer()
Destructor.
Definition: timer.cpp:56
Defintions for all general (initialisation etc.) routines of class Timer.
double endTimeInMicroSec
Store ending time in micro-second.
Definition: timer.h:76
int stopped
Stop flag.
Definition: timer.h:78
Timer()
Constructor.
Definition: timer.cpp:40
void start()
Start timer.
Definition: timer.cpp:62
timeval startCount
Definition: timer.h:84
double getElapsedTimeInMilliSec()
Get elapsed time in milli-second.
Definition: timer.cpp:109
double getElapsedTimeInMicroSec()
Get elapsed time in micro-second.
Definition: timer.cpp:88
const std::string currentDateTime()
Get current date/time, format is YYYY-MM-DD.HH:mm:ss.
Definition: timer.cpp:27
double getElapsedTime()
Get elapsed time in second.
Definition: timer.cpp:123
double startTimeInMicroSec
Store starting time in micro-second.
Definition: timer.h:74
void stop()
Stop the timer.
Definition: timer.cpp:74