forked from nathansttt/hearts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.cpp
107 lines (96 loc) · 2.54 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* $Id: timer.cpp,v 1.6 2006/11/30 20:33:25 nathanst Exp $
*
* timer.cpp
* HOG file
*
* Written by Renee Jansen on 08/28/06
*
* This file is part of HOG.
*
* HOG 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 2 of the License, or
* (at your option) any later version.
*
* HOG 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 HOG; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//#include "UnitSimulation.h"
#include "Timer.h"
#include <stdint.h>
#include <cstring>
Timer::Timer()
{
elapsedTime = 0;
}
void Timer::StartTimer()
{
#ifdef OS_MAC
startTime = UpTime();
#elif defined( TIMER_USE_CYCLE_COUNTER )
CycleCounter c;
startTime = c.count();
#else
gettimeofday( &startTime, NULL );
#endif
}
#ifdef linux
float Timer::getCPUSpeed()
{
FILE *f;
static float answer = -1;
if (answer != -1)
return answer;
f = fopen("/proc/cpuinfo", "r");
if (f)
{
while (!feof(f))
{
char entry[1024];
char temp[1024];
fgets(entry, 1024, f);
if (strstr(entry, "cpu MHz"))
{
// cpu MHz : 997.399
float answer;
sscanf(entry, "%[^0-9:] : %f", temp, &answer);
//printf("Read CPU speed: %1.2f\n", answer);
fclose(f);
return answer;
}
}
fclose(f);
}
return 0;
}
#endif
double Timer::EndTimer()
{
#ifdef OS_MAC
AbsoluteTime stopTime = UpTime();
Nanoseconds diff = AbsoluteDeltaToNanoseconds(stopTime, startTime);
uint64_t nanosecs = UnsignedWideToUInt64(diff);
//cout << nanosecs << " ns elapsed (" << (double)nanosecs/1000000.0 << " ms)" << endl;
return elapsedTime = (double)(nanosecs/1000000000.0);
#elif defined( TIMER_USE_CYCLE_COUNTER )
Timer::CycleCounter c;
double diffTime = (double)(c.count() - startTime);
const static double ClocksPerSecond = getCPUSpeed() * 1000000.0;
elapsedTime = diffTime / ClocksPerSecond;
return elapsedTime;
#else
struct timeval stopTime;
gettimeofday( &stopTime, NULL );
uint64_t microsecs = stopTime.tv_sec - startTime.tv_sec;
microsecs = microsecs * 1000000 + stopTime.tv_usec - startTime.tv_usec;
elapsedTime = (double)microsecs / 1000000.0;
return elapsedTime;
#endif
}