C++ 为什么 CLOCKS_PER_SEC 不是每秒的实际时钟数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10455905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is CLOCKS_PER_SEC not the actual number of clocks per second?
提问by Kevin H. Lin
I have just written this short C++ program to approximate the actual number of clock ticks per second.
我刚刚编写了这个简短的 C++ 程序来估算每秒的实际时钟滴答数。
#include <iostream>
#include <time.h>
using namespace std;
int main () {
for(int i = 0; i < 10 ; i++) {
int first_clock = clock();
int first_time = time(NULL);
while(time(NULL) <= first_time) {}
int second_time = time(NULL);
int second_clock = clock();
cout << "Actual clocks per second = " << (second_clock - first_clock)/(second_time - first_time) << "\n";
cout << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << "\n";
}
return 0;
}
When I run the program, I get output that looks like this.
当我运行程序时,我得到如下所示的输出。
Actual clocks per second = 199139
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 638164
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 610735
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 614835
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 642327
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 562068
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 605767
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 619543
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 650243
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 639128
CLOCKS_PER_SEC = 1000000
Why doesn't the actual number of clock ticks per second match up with CLOCKS_PER_SEC? They're not even approximately equal. What's going on here?
为什么每秒时钟滴答的实际数量与 CLOCKS_PER_SEC 不匹配?它们甚至不大致相等。这里发生了什么?
回答by Rob?
clock
returns the amount of time spent in your program.There are 1,000,000 clock ticks per second total*. It appears that your program consumed 60% of them.
clock
返回在程序中花费的时间。每秒总共有 1,000,000 个时钟滴答*。看来您的程序消耗了其中的 60%。
Something else used the other 40%.
其他东西使用了另外 40%。
*Okay, there are virtually1,000,000 clock ticks per second. The actual number is normalized so your program perceives 1,000,000 ticks.
*好的,每秒几乎有1,000,000 个时钟滴答。实际数字已标准化,因此您的程序可以感知 1,000,000 个滴答声。
回答by Daniel Fischer
From the man page of clock(3)
:
从手册页clock(3)
:
POSIX requires that CLOCKS_PER_SEC equals 1000000 independent of the actual resolution.
POSIX 要求 CLOCKS_PER_SEC 等于 1000000,与实际分辨率无关。
Your implementation seems to follow POSIX at least in that respect.
您的实现似乎至少在这方面遵循 POSIX。
Running your program here, I get
在这里运行你的程序,我明白了
Actual clocks per second = 980000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 1000000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 990000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 1000000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 1000000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 1000000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 1000000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 1000000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 1000000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 1000000
CLOCKS_PER_SEC = 1000000
or similar output on an idle machine, and output like
或闲置机器上的类似输出,并输出类似
Actual clocks per second = 50000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 600000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 530000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 580000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 730000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 730000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 600000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 560000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 600000
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 620000
CLOCKS_PER_SEC = 1000000
on a busy machine. Since clock()
measures the (approximate) time spent in your program, it seems that you tested on a busy machine, and your program got only about 60% of the CPU time.
在繁忙的机器上。由于clock()
测量了程序中花费的(大约)时间,因此您似乎是在一台繁忙的机器上进行测试,而您的程序仅获得了大约 60% 的 CPU 时间。
回答by ImanKh
- CLOCKS_PER_SECOND in POSIX is a constant equal to 1000000.
- CLOCKS_PER_SECOND is not supposed to show the number of clock in your process. It is a resolution number that you may use to convert the number of clocks to amount of time.(See man page for clock() function)
- POSIX 中的 CLOCKS_PER_SECOND 是一个等于 1000000 的常数。
- CLOCKS_PER_SECOND 不应该显示您的进程中的时钟数。这是一个分辨率数字,您可以使用它来将时钟数转换为时间量。(请参阅手册页了解时钟()函数)
For example if you calculate:
例如,如果您计算:
(second_clock-first_clock)/CLOCKS_PER_SEC
(second_clock-first_clock)/CLOCKS_PER_SEC
you will get total time between first and second call to "clock()" function.
您将获得第一次和第二次调用“clock()”函数之间的总时间。
回答by TonyK
Well duh. You don't know how far into the current second you start the timing, do you? So you can get any result from 1 to CLOCKS_PER_SEC. Try this in your inner loop:
嗯嗯。您不知道您开始计时的当前秒数,是吗?所以你可以得到从 1 到 CLOCKS_PER_SEC 的任何结果。在你的内部循环中试试这个:
int first_time = time(NULL);
// Wait for timer to roll over before starting clock!
while(time(NULL) <= first_time) {}
int first_clock = clock();
first_time = time(NULL);
while(time(NULL) <= first_time) {}
int second_time = time(NULL);
int second_clock = clock();
cout << "Actual clocks per second = " << (second_clock - first_clock)/(second_time - first_time) << "\n";
cout << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << "\n";
See ideonefor full source code. It reports actual clocks per second as 1000000, as you would expect. (I had to reduce the number of iterations to 2, so that ideone didn't time out.)
有关完整源代码,请参阅ideone。正如您所期望的那样,它报告每秒实际时钟数为 1000000。(我不得不将迭代次数减少到 2,这样 ideone 就不会超时。)