gpt4 book ai didi

c++ - 在 Windows 10 中将 CLOCKS_PER_SEC 重新定义为更高的数字

转载 作者:行者123 更新时间:2023-11-27 22:35:17 34 4
gpt4 key购买 nike

Windows 10 中的 GNU C++ 编译器返回 CLOCKS_PER_SEC = 1000,但我需要测量低于毫秒间隔的算法的编译时间(这是一个学校项目)。有没有办法将 CLOCKS_PER_SEC 重新定义为一百万(例如基于 UNIX 的操作系统)?另外,#define CLOCKS_PER_SEC ((clock_t)(1000000)) 似乎也不起作用。

最佳答案

简短回答:

长答案:否但是您可以使用QueryPerformanceCounter function ,这里是 MSDN 的一个例子:

LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;

QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartingTime);

// Activity to be timed

QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;


//
// We now have the elapsed number of ticks, along with the
// number of ticks-per-second. We use these values
// to convert to the number of elapsed microseconds.
// To guard against loss-of-precision, we convert
// to microseconds *before* dividing by ticks-per-second.
//

ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;

那样的话,您甚至可以测量纳秒,但要注意:在这种精度级别,即使是滴答计数也会漂移和抖动,因此您可能永远不会收到完美准确的结果。如果你想要完美的精度,我想你将被迫使用 RTOS在适当的专用硬件上屏蔽soft errors , 例如

关于c++ - 在 Windows 10 中将 CLOCKS_PER_SEC 重新定义为更高的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55274917/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com