-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproc_time.cpp
More file actions
71 lines (63 loc) · 1.91 KB
/
proc_time.cpp
File metadata and controls
71 lines (63 loc) · 1.91 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
// 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 ;
}
//****************************************************************************
uint calc_elapsed_time(bool done)
{
static u64 ti = 0 ;
uint secs = 0 ;
if (!done) {
ti = proc_time() ;
} else {
u64 tf = proc_time() ;
secs = (uint) ((tf - ti) / get_clocks_per_second()) ;
// syslog("send_serial_msg: %u seconds", secs) ;
}
return secs;
}
//****************************************************************************
uint calc_elapsed_msec(bool done)
{
static u64 ti = 0 ;
uint secs = 0 ;
if (!done) {
ti = proc_time() ;
} else {
u64 tf = proc_time() ;
secs = (uint) ((tf - ti) / (get_clocks_per_second()/1000)) ;
// syslog("send_serial_msg: %u seconds", secs) ;
}
return secs;
}
//****************************************************************************
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;
}