-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cpp
88 lines (77 loc) · 2.76 KB
/
Timer.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//==============================================================================
// Copyright (C) John-Philip Taylor
//
// This file is part of the EEE4084F Course
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//==============================================================================
#include "Timer.h"
//------------------------------------------------------------------------------
#ifdef __WIN32__
#include <windows.h>
#else
#include <time.h>
#endif
//------------------------------------------------------------------------------
static double Frequency;
static double Start;
//------------------------------------------------------------------------------
void tic(){
static bool First = true;
#ifdef __WIN32__
LARGE_INTEGER Time;
if(First){
QueryPerformanceFrequency(&Time);
Frequency = Time.QuadPart;
printf("Clock resolution: %lg ns\n", 1e9/Frequency);
First = false;
}
QueryPerformanceCounter(&Time);
Start = (double)Time.QuadPart / Frequency;
#else
// This is an alternative, using <sys/time.h>. It is not as accurate though.
// timeval Time;
// gettimeofday(&Time, 0);
// Start = (double)(Time.tv_sec) + (double)(Time.tv_usec)*1e-6;
timespec Time;
if(First){
clock_getres(CLOCK_MONOTONIC, &Time);
Start = (double)(Time.tv_sec) + (double)(Time.tv_nsec)*1e-9;
//printf("Clock resolution: %lg ns\n", Start*1e9);
First = false;
}
clock_gettime(CLOCK_MONOTONIC, &Time);
Start = (double)(Time.tv_sec) + (double)(Time.tv_nsec)*1e-9;
#endif
}
//------------------------------------------------------------------------------
double toc(){
double End;
#ifdef __WIN32__
LARGE_INTEGER Time;
QueryPerformanceCounter(&Time);
End = (double)Time.QuadPart / Frequency;
return End - Start;
#else
//timeval Time;
//gettimeofday(&Time, 0);
//End = (double)(Time.tv_sec) + (double)(Time.tv_usec)*1e-6;
timespec Time;
clock_gettime(CLOCK_MONOTONIC, &Time);
End = (double)(Time.tv_sec) + (double)(Time.tv_nsec)*1e-9;
return End - Start;
#endif
}
//------------------------------------------------------------------------------