3

Am I doing it correctly? At times, my program will print 2000+ for the chrono solution and it always prints 1000 for the CLOCKS_PER_SEC..

What is that value I'm actually calculating? Is it Clocks Per Sec?

#include <iostream>
#include <chrono>
#include <thread>
#include <ctime>

std::chrono::time_point<std::chrono::high_resolution_clock> SystemTime()
{
    return std::chrono::high_resolution_clock::now();
}

std::uint32_t TimeDuration(std::chrono::time_point<std::chrono::high_resolution_clock> Time)
{
    return std::chrono::duration_cast<std::chrono::nanoseconds>(SystemTime() - Time).count();
}

int main()
{
    auto Begin = std::chrono::high_resolution_clock::now();
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    std::cout<< (TimeDuration(Begin) / 1000.0)<<std::endl;

    std::cout<<CLOCKS_PER_SEC;
    return 0;
}
Brandon
  • 22,723
  • 11
  • 93
  • 186
  • Huh? What do you mean clocks per second? – happy coder Jan 23 '13 at 03:47
  • Why is clocks_per_sec macro = to 1000? and how do I calculate the amount of cycles per second my CPU does? I'm trying to write a program that does something every tick and want as much accuracy as possible without using ASM. – Brandon Jan 23 '13 at 03:49
  • I might suggest you use a hardware solution which you can read by the software portion of your project if you want better accuracy. CHEERS – happy coder Jan 23 '13 at 04:39

2 Answers2

5

In order to get the correct ticks per second on Linux, you need to use the return value of ::sysconf(_SC_CLK_TCK) (declared in the header unistd.h), rather than the macro CLOCKS_PER_SEC.

The latter is a constant defined in the POSIX standard – it is unrelated to the actual ticks per second of your CPU clock. For example, see the man page for clock:

C89, C99, POSIX.1-2001. POSIX requires that CLOCKS_PER_SEC equals 1000000 independent of the actual resolution.

However, note that even when using the correct ticks-per-second constant, you still won't get the number of actual CPU cycles per second. "Clock tick" is a special unit used by the CPU clock. There is no standardized definition of how it relates to actual CPU cycles.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
  • 1
    +1. I also thought it might be worth mentioning that many modern CPUs also have pretty complex PLL circuitry that allows for dynamic change of frequency and cut off of certain sub-parts of the CPU. –  Jan 23 '13 at 04:22
  • :l I accepted this answer.. Still.. I wanted to know how to calculate Clocks_Per_Sec.. I don't like the fact that it's defined as 1000 on windows by default. – Brandon Jan 23 '13 at 04:24
  • @CantChooseUsernames I am very far from being an expert on Windows, but it seems on Windows, `CLOCKS_PER_SEC` is the best you can get. Boost's [chrono implementation for Windows](http://www.boost.org/doc/libs/1_51_0/boost/chrono/detail/inlined/win/process_cpu_clocks.hpp) uses it as well. The idea is that the operating system ensures that `clock()` returns numbers which, when multiplied by `CLOCKS_PER_SEC`, are equivalent to the number of seconds passed. The fact that `CLOCKS_PER_SEC` is always 1000 does not necessarily contradict this. – jogojapan Jan 23 '13 at 07:22
  • 1
    Also note that, while `clock()` is supposed to return CPU time (i.e. an amount that is equivalent to how much _your_ process used the CPU), on Windows this is said to be broken and apparently returns real time, not CPU time. At least this is what people say in this SO post: http://stackoverflow.com/questions/12244153/behaviour-of-clocks-per-sec-in-different-operating-systems (see the comments for the answers) – jogojapan Jan 23 '13 at 07:24
  • I don't think, this is a right answer. Kindly refer this [link](https://stackoverflow.com/questions/19919881/sysconf-sc-clk-tck-what-does-it-return). – surendra Allam Apr 27 '23 at 13:47
0

In boost's library, there is a timer class, use CLOCKS_PER_SEC to calculate the maximum time the timer can elapse. It said that on Windows CLOCKS_PER_SEC is 1000 and on Mac OS X, Linux it is 1000000. So on the latter OSs, the accuracy is higher.

hongbin pei
  • 21
  • 2
  • 5