-
Notifications
You must be signed in to change notification settings - Fork 5
/
stopwatch.cpp
45 lines (38 loc) · 987 Bytes
/
stopwatch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "stopwatch.h"
StopWatch::StopWatch() :
started(false)
{
}
void StopWatch::start(void)
{
started = true;
clock_gettime(CLOCK_MONOTONIC, &starttime);
}
long StopWatch::elapsedmsec(void)
{
if(!started) return -1;
timespec tmp;
clock_gettime(CLOCK_MONOTONIC, &tmp);
tmp = timedifference(starttime,tmp);
return tmp.tv_sec*1000+tmp.tv_nsec/1000000;
}
float StopWatch::elapsedsec(void)
{
if(!started) return -1;
timespec tmp;
clock_gettime(CLOCK_MONOTONIC, &tmp);
tmp = timedifference(starttime,tmp);
return tmp.tv_sec + (float)tmp.tv_nsec/1000000000;
}
timespec StopWatch::timedifference(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}