Google Answers Logo
View Question
Q: studies properites of the clock in computer ( Answered,   0 Comments )
Subject: studies properites of the clock in computer
Category: Computers > Programming
Asked by: damonguitar-ga
List Price: $3.00
Posted: 05 Oct 2002 15:29 PDT
Expires: 04 Nov 2002 14:29 PST
Question ID: 72997
the following program named "studies of the clock  in computer" runs
properly in microsoft C++ under cpu 800 Mhz.the question is that i
need someone to explain the output data clearly and what do those data
represent for. also, just wondering that why the last line of output
"CLK_TCK and CLOCKS_PER_SEC" are equal to zero because they should
contain some numbers!!!

void main(){
	long i,j,k,t1,t2,t;
	double time;
	float x;
	int p=123;
	printf("tests the clock by running dummy loops of various

	printf("\n i     k t1 t2 t    time\n\n");
		printf("%31d %101d %41d %41d %41d %9.4f \n", i,k,t1,t2,t,time,p);
	printf("\n  CLK_TCK=%.4f, CLOCKS_PER_SEC=%.4f \n", CLK_TCK,
Subject: Re: studies properites of the clock in computer
Answered By: nauster-ga on 05 Oct 2002 23:40 PDT
This program is intended to demonstrate how long it takes the CPU to
perform an empty loop many times over. The line:


Does nothing but start j at 1 and increment it over and over until it
gets to the value of k. The printf near the bottom is intended to
output the number of increments (loops) it did, and the total time
elapsed. This test is run 500 times, and each time the value of k is
increased 20% over the prior test.

There are, however, several things wrong with the program.

Most importantly, it is often assigning an integer value to a
floating-point type variable without explicitly saying that its doing
this. This is causing the problem with the zeros. Whenever you want to
store or display an integer value, an integer-type variable should be

In Microsoft C++, CLK_TCK and CLOCKS_PER_SEC are set to the same
value, which is 1000, and integer value. Attempting to print an
integer value with a floating-point specifier in the printf is bad.
You're not going to see the proper value.

There are other problems here and there. I rewrote it as follows:

void main(){ 
 long i,j,k,t1,t2,t; 
 double time; 
 printf("tests the clock by running dummy loops of various
 k = 10000;

 printf("\n i     k t1 t2 t    time\n\n"); 
  time= (float)t/CLK_TCK; 
  printf("%4d %10d %6d %6d %6d %2.4f \n", i,k,t1,t2,t,time); 
 printf("\n  CLK_TCK=%d, CLOCKS_PER_SEC=%d \n", CLK_TCK, 

Now, a sample line of output looks like this:
47  52659768 2078 2468 390  0.3900

47 = which test # this is
52659768 = how many loops it will try this time
2078 = timer reading at start of loops
2468 = timer reading at end of loops
390  = how much time in timer ticks it took (2468 - 2078 = 390)
0.39 = how much time in seconds that it took

Since CLOCK_PER_SEC is 1000, that means every 1000 timer ticks is one
second. Which means 390 timer ticks is .39 seconds.

All this means my computer does the loop 52659768 times in .39

Please ask for clarification on anything that is not totally clear to


Request for Answer Clarification by damonguitar-ga on 06 Oct 2002 11:15 PDT
can u please explain the detail of clock() function

Clarification of Answer by nauster-ga on 06 Oct 2002 14:04 PDT
In Microsoft's product, it returns the amount of time, measured in
1/CLOCKS_PER_SEC, that has elapsed since the start of the program.
Since CLOCKS_PER_SEC is 1000, that means it is returning the number of
milliseconds since the program started.

It is not precise to the millisecond, however. It provides only an
approximation. For example, when I run it continuously and have it
report its results, it jumps from 0 to 15 to 31. As long as you keep
its imprecision in mind, it can be quite useful for measuring elapsed

Other compilers have slightly different versions of the clock()
function, but in all cases the intent is to provide some way for the
programmer to have access to the amount of real-world time has elapsed
while the program is running.
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy