首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么CLOCKS_PER_SEC不是每秒的实际时钟数?

为什么CLOCKS_PER_SEC不是每秒的实际时钟数?
EN

Stack Overflow用户
提问于 2012-05-04 20:43:58
回答 5查看 66.5K关注 0票数 33

我刚刚编写了一个简短的C++程序,以估计每秒时钟滴答的实际数量。

代码语言:javascript
复制
#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;

}

当我运行这个程序时,我得到的输出如下所示。

代码语言:javascript
复制
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不匹配?他们甚至都不平等。这里发生了什么事?

EN

回答 5

Stack Overflow用户

发布于 2012-05-04 20:47:13

来自clock(3)的手册页

POSIX要求CLOCKS_PER_SEC与实际分辨率无关,等于1000000。

至少在这方面,您的实现似乎遵循了POSIX。

在这里运行你的程序,我得到

代码语言:javascript
复制
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

或类似于空闲机器上的输出,并且输出如下

代码语言:javascript
复制
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时间。

票数 21
EN

Stack Overflow用户

发布于 2015-12-09 22:31:58

  1. CLOCKS_PER_SECOND在POSIX中是一个常数,等于1000000。
  2. CLOCKS_PER_SECOND不应该显示进程中的时钟数。它是一个可用于将时钟数量转换为时间量的分辨率数字。(关于clock()函数,参见手册页)

例如,如果您计算:

(第二个时钟-第一个时钟)/CLOCKS_PER_SEC

您将得到第一次和第二次调用"clock()“函数之间的总时间。

票数 6
EN

Stack Overflow用户

发布于 2016-04-16 13:10:46

C99标准

C99 N1256标准草案CLOCKS_PER_SEC唯一的评价是:

CLOCKS_PER_SEC,它扩展为clock_t类型的表达式(如下所述),即时钟函数返回的值的每秒数。

正如其他人所提到的,POSIX将其设置为100万,这将其精度限制在1微秒。我认为这只是一个历史值,从天的最大CPU频率被测量的Mega Hertz。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10455905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档