# Precision of clock()

• May 26th, 2010, 03:22 PM
nefzaral
Precision of clock()
Hey!

I'm fairly new to programming in general and forums (never been a member before!). I tried finding something on this topic, but failed to find anything.

So.. I've written the following code and was wondering if the clock funciton, along with CLOCKS_PER_SEC is indeed accurate to 10 decimal places. I find that a little hard to believe. Are there any other functions in C++ that allow you to write code with respect to time?

Code:

```#include<ctime> #include<iostream> using namespace std; int main(void){         //Reads in one random double in the range [0,1> to         //"kort" every 1/fs seconds. Writes the time elapsed         //for evert 1000th sample. Returns the total time elapsed         //since the operation began. Expected return value is         //equal to n.                 int n = 3;         int fs = 44100;         double* kort = new double[fs*n];         //Get initial value of clock ticks since "main" started         double start = clock();         //Note: "i++" missing from "for" statement         for(int i=0; i<fs*n;){                 //Get the number of times the clock has been incremented                 //since the initial value was set                 double interval = clock() - start;                 //Checks if the clock har been incremented enough times                 //to be sure that 1/fs seconds have gone by                 if(interval/CLOCKS_PER_SEC >= (double)(i+1)/fs){                         //Generate random double in range [0,1>                         kort[i] = (double)rand()/(double)(RAND_MAX+1);                         //Cannot write for every sample, due to the latency                         //introduced by "cout" operation                         if(i%1000==0)                                 cout<< interval/CLOCKS_PER_SEC << endl;                                                 //Increment i to continue the loop                         i++;                 }                 //i remains the same if if-satement returns false                 //because kort[i] has not been initialized with                 //a valid value yet. This is because the next sample                 //will not have been passed to the program if if-statement                 //returns false.         }         //Gets total amount of times clock has been incremented since         //the beginning of the program.         double timeElapsed2 = clock()-start;         //Converts number of clock incrementations to seconds         timeElapsed2 = timeElapsed2/CLOCKS_PER_SEC;         //Returns number of seconds the operation has taken         cout<< "Operation took " << timeElapsed2 << " seconds to complete.\n";         if(abs(timeElapsed2-(double)n) < 1e-9)                 cout<< "Expectations have been met.\n";         else         cout<< "Expectations have not been met. Optimize code further.\n";         return 0; }```
• May 26th, 2010, 04:37 PM
Speedo
Re: Precision of clock()
Quote:

So.. I've written the following code and was wondering if the clock funciton, along with CLOCKS_PER_SEC is indeed accurate to 10 decimal places.
10 decimal places, as in accurate to 0.0000000001s? 1/10th of a nanosecond? No, it's nowhere close.

clock() does nothing but record the number of clock ticks since the program started. So the maximum possible accuracy would be 1/CLOCKS_PER_SECOND - for me on 32 bit WinXP CLOCKS_PER_SECOND is defined as 1000, so the smallest numbers it will give you will be milliseconds (10^-3). As for its resolution - I have no idea offhand, but I wouldn't use it for any kind of accurate timing.

If you really want precise timing, you need to go with platform specific functions. On windows that's QueryPerformanceCounter. How precise it is depends on your actual CPU, but I think it can be accurate to 2-3 microseconds on most modern CPUs.

Edit: Maybe even more accurate than that. This program will check the frequency of the high resolution timer on your system-

Code:

```#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <iostream> #include <iomanip> int main() {   LARGE_INTEGER freq;   QueryPerformanceFrequency(&freq);   std::cout << "Clock Frequency: " << freq.QuadPart << std::endl;   std::cout.precision(15);   std::cout << std::fixed << "In seconds: " << 1.0 / static_cast<double>(freq.QuadPart) << std::endl;   return 0; }```
On my relatively old (Athlon64x2 4400) CPU the results are

Clock Frequency: 3579545
In seconds: 0.000000279365115
• May 26th, 2010, 06:04 PM
dude_1967
Re: Precision of clock()
Yes.

In addition, Intel and Intel-like processors offer a hardware counter which counts the number of CPU oscillations since power-up.

Assembler sequences (which are highly dependent on the platform and compiler) exist for reading these high performance tick counts. You can search for the terms "cpuid" and "rdtsc", which are the names of relevant Intel assembler opcodes in this matter.

We covered this rather large topic very briefly a while ago here: