精确计算计算机时钟
精确计算计算机时钟
NiuYoohoo 发表于6个月前
精确计算计算机时钟
  • 发表于 6个月前
  • 阅读 12
  • 收藏 0
  • 点赞 0
  • 评论 0

获取定时器频率

Syntax

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

Parameters

[out] Pointer to a variable that receives the current performance-counter frequency, in counts per second. If the installed hardware does not support a high-resolution performance counter, this parameter can be zero.

Return Value

If the installed hardware supports a high-resolution performance counter, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. For example, if the installed hardware does not support a high-resolution performance counter, the function fails.

Description

The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. The frequency cannot change while the system is running.

获取定时器数值

Syntax

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);

Parameters

[out] Pointer to a variable that receives the current performance-counter value, in counts.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

Description

The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter.

#注意选项
#On a multiprocessor computer, it should not matter which processor is called. However,
#you can get different results on different processors due to bugs in the 
#basic input/output system (BIOS) or the hardware abstraction layer (HAL). 
#To specify processor affinity for a thread, use the SetThreadAffinityMask function. 
#=============================================================================================
#在多处理器计算机上,不应该考虑调用了哪个处理器。但是,由于基本输入/输出系统(BIOS)或硬件抽象层(HAL)的bug
#你可能在不同的处理器上得到不同的结果。指定一个线程处理器亲和性,使用setthreadaffinitymask功能。
#就可以避免这个bug
#DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask);
#简单来说就是设置线程到指定CPU运行。SetThreadAffinityMask(hThread0, 0x00000001);//限制到CPU1上运行

Example

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
	LARGE_INTEGER freq;
	LARGE_INTEGER start;
	LARGE_INTEGER end;
	SetThreadAffinityMask(GetCurrentThread(), 0x1);
	QueryPerformanceFrequency(&freq);
	QueryPerformanceCounter(&start);
	Sleep(1789);
	QueryPerformanceCounter(&end);
	
	cout << ((end.QuadPart - start.QuadPart) / (double)(freq.QuadPart));
	return 0;
}
/*
直接用代码区别define和typedef,define为简单的替换所以_UNAME被替换为空白
所以第一位匿名结构体,方便用户直接读取联合体中结构体中的数据若支持64位可以
直接使用QuadPart,不支持的话则自行通过结构体装换使用。
*/
#define _UNAME
typedef union _LARGE_INTEGER 
{
	struct 
	{
		DWORD LowPart;
		LONG HighPart;
	} _UNAME;
	struct 
	{
		DWORD LowPart;
		LONG HighPart;
	} u;
	LONGLONG QuadPart;
} LARGE_INTEGER;

获取操作系统时钟

Syntax

DWORD WINAPI GetTickCount(void);

Return Value

The return value is the number of milliseconds that have elapsed since the system was started.

#The resolution is limited to the resolution of the system timer. 
#This value is also affected by adjustments made by the GetSystemTimeAdjustment function.
#The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero 
#if the system is run continuously for 49.7 days. To avoid this problem, use GetTickCount64.
#Otherwise, check for an overflow condition when comparing times.

获取进程时钟

Syntax

clock_t clock( void );

Return Value

The elapsed wall-clock time since the start of the process (elapsed time in seconds times CLOCKS_PER_SEC). If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.

Description

The clock function tells how much time the calling process has used. A timer tick is approximately equal to 1/CLOCKS_PER_SEC second. In versions of Microsoft C before 6.0, the CLOCKS_PER_SEC constant was called CLK_TCK.

标签: 时钟
共有 人打赏支持
粉丝 2
博文 44
码字总数 18104
×
NiuYoohoo
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: