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?


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
				cout<< interval/CLOCKS_PER_SEC << endl;
			//Increment i to continue the loop
		//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";
	cout<< "Expectations have not been met. Optimize code further.\n";

	return 0;
Thanks for your time.