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

Kev*_*Lin 25 c++ clock system-clock

我刚刚编写了这个简短的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;

}
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,我得到的输出看起来像这样.

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
Run Code Online (Sandbox Code Playgroud)

为什么每秒的实际时钟周期数与CLOCKS_PER_SEC不匹配?它们甚至不大致相同.这里发生了什么?

Rob*_*obᵩ 34

clock返回您在程序中花费的时间.每秒有1,000,000个时钟滴答*.您的程序似乎消耗了60%的程序.

其他东西使用其他40%.

*好的,每秒几乎有 1,000,000个时钟滴答.实际数字是标准化的,因此您的程序可以感知1,000,000个滴答.


Dan*_*her 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
Run Code Online (Sandbox Code Playgroud)

或空闲机器上的类似输出,输出如

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
Run Code Online (Sandbox Code Playgroud)

在繁忙的机器上.由于clock()测量了程序中花费的(近似)时间,似乎您在繁忙的机器上进行了测试,并且您的程序只获得了大约60%的CPU时间.

  • 虽然这在技术上是正确的,但由于他正在使用的系统调用也尊重它是无用的. (5认同)

Ima*_*nKh 6

  1. POSIX 中的 CLOCKS_PER_SECOND 是一个等于 1000000 的常数。
  2. CLOCKS_PER_SECOND 不应该显示您的进程中的时钟数。这是一个分辨率数字,您可以使用它来将时钟数转换为时间量。(请参阅手册页了解时钟()函数)

例如,如果您计算:

(second_clock-first_clock)/CLOCKS_PER_SEC

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