Repository navigation
Expand file tree
/
Copy pathproc_time.cpp
More file actions
75 lines (66 loc) · 1.93 KB
/
Copy pathproc_time.cpp
File metadata and controls
75 lines (66 loc) · 1.93 KB
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
// Measure elapsed time using QueryPerformanceCounter()
// build: g++ -Wall -O2 proc_time.cpp -o proc_time.exe
#include <windows.h>
#include <stdio.h>
typedef unsigned int uint ;
typedef unsigned long long u64 ;
//*****************************************************************************
u64 proc_time(void)
{
// return (unsigned) clock() ;
LARGE_INTEGER ti ;
QueryPerformanceCounter(&ti) ;
return (u64) ti.QuadPart ;
}
//*************************************************************************
u64 get_clocks_per_second(void)
{
static u64 clocks_per_sec64 = 0 ;
if (clocks_per_sec64 == 0) {
LARGE_INTEGER tfreq ;
QueryPerformanceFrequency(&tfreq) ;
clocks_per_sec64 = (u64) tfreq.QuadPart ;
}
return clocks_per_sec64 ;
}
//*************************************************************************
u64 get_clocks_per_msec(void)
{
return get_clocks_per_second() / 1000 ;
}
//****************************************************************************
u64 calc_elapsed_time(bool done)
{
static u64 ti = 0 ;
u64 secs = 0 ;
if (!done) {
ti = proc_time() ;
} else {
u64 tf = proc_time() ;
secs = (tf - ti) / get_clocks_per_second() ;
}
return secs;
}
//****************************************************************************
u64 calc_elapsed_msec(bool done)
{
static u64 ti = 0 ;
u64 msecs = 0 ;
if (!done) {
ti = proc_time() ;
} else {
u64 tf = proc_time() ;
msecs = (tf - ti) / get_clocks_per_msec() ;
}
return msecs;
}
//****************************************************************************
int main(void)
{
printf("measuring time via QueryPerformanceCounter()\n");
calc_elapsed_msec(false); // initialize counter
SleepEx(2000, false);
uint msecs = calc_elapsed_msec(true);
printf("Elapsed time: %u msecs\n", msecs);
return 0;
}