VirtualBox

Ticket #8707: timetest.cpp

File timetest.cpp, 2.3 KB (added by ricebowl, 14 years ago)

Test program

Line 
1#define _CRT_SECURE_NO_WARNINGS
2#define WIN32_LEAN_AND_MEAN
3#include <windows.h>
4#include <malloc.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <stddef.h>
8
9
10typedef unsigned __int8 UI8;
11typedef signed __int8 SI8;
12typedef unsigned __int16 UI16;
13typedef signed __int16 SI16;
14typedef unsigned __int32 UI32;
15typedef signed __int32 SI32;
16typedef unsigned __int64 UI64;
17typedef signed __int64 SI64;
18
19
20typedef SI32 (NTAPI *PFN_NT_QUERY_PERFORMANCE_COUNTER)(OUT PLARGE_INTEGER PerformanceCounter, OUT PLARGE_INTEGER PerformanceFrequency OPTIONAL);
21
22
23int __cdecl main(void)
24{
25 LARGE_INTEGER PerformanceCounter, PerformanceFrequency;
26 HMODULE Ntdll;
27 PFN_NT_QUERY_PERFORMANCE_COUNTER NtQueryPerformanceCounter;
28 UI64 StartTime, StartTick;
29 SI32 Status;
30
31 Ntdll = GetModuleHandleW(L"ntdll.dll");
32 NtQueryPerformanceCounter = (PFN_NT_QUERY_PERFORMANCE_COUNTER) GetProcAddress(Ntdll, "NtQueryPerformanceCounter");
33
34 // Take an initial sample of two time sources: the TSC and the HPET
35 Status = NtQueryPerformanceCounter(&PerformanceCounter, &PerformanceFrequency);
36 if (Status < 0)
37 {
38 printf("NtQueryPerformanceCounter(): %08X\n", Status);
39 return 0;
40 }
41
42 StartTick = __rdtsc();
43 StartTime = PerformanceCounter.QuadPart;
44
45 printf("NtQueryPerformanceCounter(): CTR=0x%016llX FRQ=0x%016llX\r\n", PerformanceCounter.QuadPart, PerformanceFrequency.QuadPart);
46
47 for(size_t i = 0; i < 60; i++)
48 {
49 UI64 EndTime, EndTick, DeltaTime, DeltaTick, TickRate;
50
51 // Sleep for a while.
52 Sleep(1000);
53
54 // Sample the two clocks again. N.B. rdtsc is sampled *first* this time.
55 EndTick = __rdtsc();
56 Status = NtQueryPerformanceCounter(&PerformanceCounter, &PerformanceFrequency);
57 if (Status < 0)
58 {
59 printf("NtQueryPerformanceCounter(): %08X\n", Status);
60 return 0;
61 }
62
63 EndTime = PerformanceCounter.QuadPart;
64
65 printf("NtQueryPerformanceCounter(): CTR=0x%016llX FRQ=0x%016llX\r\n", PerformanceCounter.QuadPart, PerformanceFrequency.QuadPart);
66
67 DeltaTime = EndTime - StartTime;
68 DeltaTick = EndTick - StartTick;
69 TickRate = (DeltaTick * 10000000ull) / DeltaTime;
70
71 printf("[%2u] TIME=+%llu.%03u TICKS=+%llu (%llu ticks/s)\r\n", i, DeltaTime / 10000000ull, (unsigned int)(DeltaTime % 10000000ull) / 10000, DeltaTick, TickRate);
72
73 EndTick += DeltaTick;
74 EndTime += DeltaTime;
75 }
76
77 return 0;
78}

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette