Why is CLOCKS_PER_SEC not the actual number of clocks per second?

后端 未结 6 1586
情话喂你
情话喂你 2020-11-30 03:53

I have just written this short C++ program to approximate the actual number of clock ticks per second.

#include 
#include 

usi         


        
6条回答
  •  悲哀的现实
    2020-11-30 04:48

    From the man page of clock(3):

    POSIX requires that CLOCKS_PER_SEC equals 1000000 independent of the actual resolution.

    Your implementation seems to follow POSIX at least in that respect.

    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.

提交回复
热议问题