我刚刚编写了这个简短的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;
}
当我运行程序时,我得到的输出看起来像这样。
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
为什么每秒的实际时钟滴答数与CLOCKS_PER_SEC不匹配?它们甚至不大致相同。这是怎么回事?
答案 0 :(得分:31)
clock
返回在您的程序中花费的时间。每秒有1,000,000个时钟节拍 * 。您的程序似乎消耗了60%。
其他东西使用了其他40%。
* 好的,每秒有几乎 1,000,000个时钟滴答。实际数字已经标准化,因此您的程序可以感知1,000,000个滴答。
答案 1 :(得分:20)
来自clock(3)
的手册页:
POSIX要求CLOCKS_PER_SEC等于1000000,与实际分辨率无关。
您的实施似乎至少在这方面遵循POSIX。
在这里运行你的程序,我得到了
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
或空闲机器上的类似输出,输出如
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
在繁忙的机器上。由于clock()
测量程序中花费的(近似)时间,因此您似乎在繁忙的计算机上进行了测试,并且您的程序只获得了大约60%的CPU时间。
答案 2 :(得分:3)
例如,如果你计算:
(second_clock-first_clock)/ CLOCKS_PER_SEC
您将获得第一次和第二次调用“clock()”函数之间的总时间。
答案 3 :(得分:1)
C99标准
C99 N1256 standard draft对CLOCKS_PER_SEC
所说的唯一内容是:
CLOCKS_PER_SEC 它扩展为类型为clock_t(如下所述)的表达式 时钟函数返回值的每秒数
正如其他人所说,POSIX将其设置为100万,这将其精度限制为1微秒。我认为这只是从以兆赫兹测量最大CPU频率的日子开始的历史值。
答案 4 :(得分:0)
当您将 int first_time = time(NULL); 设置为1时,time(NULL)可能“相距1纳秒”(因为它被截断了)。因此, while(time(NULL)<= first_time){} 可以跳过1秒以上,比您预期的要快。
这就是为什么您的“ 1秒”中的时钟更少的原因。
答案 5 :(得分:-2)
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";
有关完整源代码,请参阅ideone。如您所料,它将每秒实际时钟报告为1000000。 (我不得不将迭代次数减少到2,因此ideone没有超时。)