16,465
社区成员
发帖
与我相关
我的任务
分享
#include < Windows.h >
#include < MMSystem.h >
#pragma comment(lib, "winmm.lib")
DWORD x,y;
BOOL z=0;
MMRESULT r;
//设置时间间隔为1ms
r=timeBeginPeriod(1);
if(TIMERR_NOERROR==r)
cout<<"1 set ok"<<endl;
GetSystemTimeAdjustment(&x,&y,&z);
cout<<x<<" "<<y<<endl;
//设置时间间隔为10ms
r=timeBeginPeriod(10);
if(TIMERR_NOERROR==r)
cout<<"10 set ok"<<endl;
GetSystemTimeAdjustment(&x,&y,&z);
cout<<x<<" "<<y<<endl;
LARGE_INTEGER liStart, liEnd, liFreq;
// test, how much time the inc is using
QueryPerformanceCounter( & liStart);
//for(int j=0;j<100;j++)
Sleep(200);
QueryPerformanceCounter( & liEnd);
QueryPerformanceFrequency( & liFreq);
double ms=(( double )(liEnd.QuadPart - liStart.QuadPart)*1000)/( double )liFreq.QuadPart;
printf( " Inc duration: %f ms\n" , ms);
这个貌似也不要证明?
//-----------------------------------------------------------------------------------//
// Windows Graphics Programming: Win32 GDI and DirectDraw //
// ISBN 0-13-086985-6 //
// //
// Written by Yuan, Feng www.fengyuan.com //
// Copyright (c) 2000 by Hewlett-Packard Company www.hp.com //
// Published by Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com //
// //
// FileName : timer.cpp //
// Description: Compare timer accuracy, Chapter 2 //
// Version : 1.00.000, May 31, 2000 //
//-----------------------------------------------------------------------------------//
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <mmsystem.h>
#include "resource.h"
int MyMessageBox(HWND hWnd, const TCHAR * text, const TCHAR * caption, DWORD style)
{
MSGBOXPARAMS param;
memset(& param, 0, sizeof(param));
param.cbSize = sizeof(param);
param.hwndOwner = hWnd;
param.hInstance = GetModuleHandle(NULL);
param.lpszText = text;
param.lpszCaption = caption;
param.dwStyle = style | MB_USERICON;
param.lpszIcon = MAKEINTRESOURCE(IDI_GRAPH);
return MessageBoxIndirect(¶m);
}
#pragma warning(disable : 4035 )
__int64 MyQueryCounter(void)
{
_asm _emit 0x0F
_asm _emit 0x31
}
__int64 MyQueryFrequency(void)
{
__int64 start = MyQueryCounter();
Sleep(1000);
__int64 stop = MyQueryCounter();
return stop - start;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
const int SAMPLES = 32;
DWORD t1[SAMPLES], t2[SAMPLES];
__int64 t3[SAMPLES], t4[SAMPLES], freq3, freq4;
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(tc));
timeBeginPeriod(1);
QueryPerformanceFrequency((LARGE_INTEGER *) & freq3); freq3 /= 1000;
freq4 = MyQueryFrequency(); freq4 /= 1000;
for (int i=0; i<SAMPLES; i++)
{
for (int j=0; j<200; j++) // roughly 0.2 ms to 0.5ms delay
DeleteObject(CreateSolidBrush(0));
t1[i] = GetTickCount();
t2[i] = timeGetTime();
QueryPerformanceCounter((LARGE_INTEGER *) & t3[i]);
t4[i] = MyQueryCounter();
}
timeEndPeriod(1);
TCHAR buffer[1024];
sprintf(buffer, _T("tick mm %d Khz %5.1f Mhz %5.1f Mhz\n\n"), tc.wPeriodMin, freq3/1000.0, freq4/1000.0);
for (i=0; i<SAMPLES; i++)
wsprintf(buffer+ _tcslen(buffer), "%8d %8d %8d %8d\n",
(t1[i]-t1[0])*1000000,
(t2[i]-t2[0])*1000000,
(int)((t3[i]-t3[0])*1000000/freq3), (int)((t4[i]-t4[0])*1000000/freq4));
MyMessageBox(NULL, buffer, "Timer Accuracy", MB_OK);
return 0;
}