|
-
May 26th, 2010, 04:37 PM
#2
Re: Precision of clock()
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
Last edited by Speedo; May 26th, 2010 at 04:59 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|