将 CLOCKS_PER_SEC 重新定义为 Windows 中的更高数字 10
Redefining CLOCKS_PER_SEC to a higher number in Windows 10
GNU C++ 编译器 Windows 10 returns 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 on appropriate, specialized hardware which is shielded against soft errors,例如
Well, this assignment absolutely requires the usage of time.h
and time.h
only
在这种情况下,测量较短的时间很难,但是让较短的时间变长很容易...只需重复您的算法直到达到 1 秒,然后将测量的时间除以迭代次数做过。对于与缓存相关和分支预测器相关的时间,您可能会得到一个倾斜的图片(因为重复迭代将 "warm up" 缓存并教导分支预测器),但对于其余部分,它应该相当准确。
顺便说一句,请注意使用 clock()
有点问题,因为 按照标准 它测量用户 CPU 当前进程的时间(因此,内核时间和 IO 等待被排除在外),尽管在 Windows 它测量挂钟时间。只要你的算法是 CPU-bound 并且几乎连续地设法达到 运行 ,这基本上是相同的,但是如果它是 IO-bound 或者如果它是 运行在繁忙的系统上
如果您对挂钟时间感兴趣并且限于 time.h
,您最好的选择是普通的 time()
;在那种情况下,我会在忙碌的等待中精确地同步到秒的变化,然后如前所述在几秒钟内测量迭代次数。
time_t start = time(nullptr);
while(start == time(nullptr));
start = time(nullptr);
int i = 0;
while(time(nullptr) - start < 5) {
// your algorithm
++i;
}
int elapsed = time(nullptr) - start;
double time_per_iteration = double(elapsed) / i;
GNU C++ 编译器 Windows 10 returns 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 on appropriate, specialized hardware which is shielded against soft errors,例如
Well, this assignment absolutely requires the usage of
time.h
andtime.h
only
在这种情况下,测量较短的时间很难,但是让较短的时间变长很容易...只需重复您的算法直到达到 1 秒,然后将测量的时间除以迭代次数做过。对于与缓存相关和分支预测器相关的时间,您可能会得到一个倾斜的图片(因为重复迭代将 "warm up" 缓存并教导分支预测器),但对于其余部分,它应该相当准确。
顺便说一句,请注意使用 clock()
有点问题,因为 按照标准 它测量用户 CPU 当前进程的时间(因此,内核时间和 IO 等待被排除在外),尽管在 Windows 它测量挂钟时间。只要你的算法是 CPU-bound 并且几乎连续地设法达到 运行 ,这基本上是相同的,但是如果它是 IO-bound 或者如果它是 运行在繁忙的系统上
如果您对挂钟时间感兴趣并且限于 time.h
,您最好的选择是普通的 time()
;在那种情况下,我会在忙碌的等待中精确地同步到秒的变化,然后如前所述在几秒钟内测量迭代次数。
time_t start = time(nullptr);
while(start == time(nullptr));
start = time(nullptr);
int i = 0;
while(time(nullptr) - start < 5) {
// your algorithm
++i;
}
int elapsed = time(nullptr) - start;
double time_per_iteration = double(elapsed) / i;