如何证明timeBeginPeriod设置成功????

sichuanwww 2017-08-08 03:28:09


#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;


以上代码,用GetSystemTimeAdjustment都是输出156001,这个不能证明设置成功。


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);
这个貌似也不要证明?
哪位高人有好方法????????????不吝赐教!!!
...全文
1288 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sichuanwww 2017-08-08
  • 打赏
  • 举报
回复
对sleep看不出什么明显影响,不过还是谢谢大家积极回复。
schlafenhamster 2017-08-08
  • 打赏
  • 举报
回复
timeBeginPeriod和timeEndPeriod。要成对 使用
schlafenhamster 2017-08-08
  • 打赏
  • 举报
回复
15.6ms被表示为156001个100ns单位。
schlafenhamster 2017-08-08
  • 打赏
  • 举报
回复

//-----------------------------------------------------------------------------------//
//              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;
}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧