-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHigh_Resolution_Timer.cpp
64 lines (61 loc) · 1.7 KB
/
High_Resolution_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
#include<iostream>
#include<windows.h>
class CTimer{
public:
CTimer() {
QueryPerformanceFrequency(&mqFreq);
}
~CTimer() {}
void Start() {
QueryPerformanceCounter(&mqStart);
}
void End() {
QueryPerformanceCounter(&mqEnd);
}
double GetTimeInSeconds() {
return (mqEnd.QuadPart - mqStart.QuadPart)/(double)mqFreq.QuadPart;
}
double GetTimeInMilliseconds() {
return (1.0e3*(mqEnd.QuadPart - mqStart.QuadPart))/mqFreq.QuadPart;
}
double GetTimeInMicroseconds() {
return (1.0e6*(mqEnd.QuadPart - mqStart.QuadPart))/mqFreq.QuadPart;
}
double GetTimeInNanoseconds() {
return (1.0e9*(mqEnd.QuadPart - mqStart.QuadPart))/mqFreq.QuadPart;
}
private:
LARGE_INTEGER mqStart;
LARGE_INTEGER mqEnd;
LARGE_INTEGER mqFreq;
};
void Bubblesort(int iaArray[], int iLength)
{
for (int iIndex1 = 0; iIndex1 < iLength; ++iIndex1) {
for (int iIndex2 = 0; iIndex2 < iLength - iIndex1 - 1; ++iIndex2) {
if (iaArray[iIndex2] > iaArray[iIndex2 + 1]) {
int iTemp = iaArray[iIndex2];
iaArray[iIndex2] = iaArray[iIndex2 + 1];
iaArray[iIndex2 + 1] = iTemp;
}
}
}
}
int main(){
using namespace std;
// Fill an array with random integer values for sorting
const int kiLength = 10000;
int iaArray[kiLength];
for (int iIndex = 0; iIndex < kiLength; ++iIndex) {
iaArray[iIndex] = rand();
}
// Time the sort
CTimer qTimer;
qTimer.Start();
Bubblesort(iaArray, kiLength);
qTimer.End();
cout << qTimer.GetTimeInSeconds() << " seconds" << endl;
cout << qTimer.GetTimeInMilliseconds() << " milliseconds" << endl;
cout << qTimer.GetTimeInMicroseconds() << " microseconds" << endl;
cout << qTimer.GetTimeInNanoseconds() << " nanoseconds" << endl;
}