Google Answers Logo
View Question
 
Q: studies properties of the clock in computer ( Answered,   0 Comments )
Question  
Subject: studies properties of the clock in computer
Category: Computers > Programming
Asked by: damonguitar-ga
List Price: $2.00
Posted: 04 Oct 2002 11:52 PDT
Expires: 03 Nov 2002 10:52 PST
Question ID: 72527
i need someone to explain the following program "studies properties of
the clock" step by step!!!!so, i hope that i can get a good
explanation.#include<stdio.h>
#include<dos.h>
#include<time.h>
#include<iostream.h>
#include<math.h>

void main(){
	long i,j,k,t1,t2,t;
	double time;
	float x;

	printf("tests the clock by running dummy loops of various
lengths\n");

	x=1;
	printf("\n i     k t1 t2 t    time\n\n");
	
	for(i=1;i<=500;i++){
		x=x*1.2;
		k=x;
		t1=clock();
		for(j=1;j<=k;j++);
		t2=clock();
		t=t2-t1;
		time=t/clk_tck;
		printf("%31d %101d %41d %41d %41d %9.4f \n", i,k,t1,t2,t,time);
		if(time>12.)
			break;
	}
	printf("\n  clk_tck=%.4f, clocks_per_second=%.4f \n", clk_tck,
		clocks_per_sec);
}
Answer  
Subject: Re: studies properties of the clock in computer
Answered By: bookface-ga on 04 Oct 2002 14:47 PDT
 
This program simple loops through a simple process until 12 seconds
have gone by, according to the system. (There are a few minor errors;
for instance, clk_tck should be CLK_TCK, and the last value
clocks_per_sec is not defined anywhere, but should be a given value
used for comparison.) But basically, if it were compiling properly, it
should loop until the inner loop takes 12 seconds to complete,
displaying:

i k t1 t2 t time

and then the respective values for each iteration in the outer loop:

i: iteration, or number of times the loop has been executed.

k: slowly increases. x starts off as 1 and becomes 1.2 * its previous
value every iteration. k is the translation of that number by
truncating all places after the decimal point, i.e. 1.95 becomes 1,
but 2.34 becomes 2.

t1: the time, in ticks, since the program started operation, before
the second subloop (right under t1's assignment, "for(j=1;j<=k;j++);")
is executed; this loop should grow quickly, but running optimized will
probably not increase the delay significantly between rounds.

t2: the time after the loop, in ticks.

t: the difference between t1 and t2.

When the inner loop takes 12 or greater seconds to execute, the
program considers this a large enough value to obtain accurate data
from. Then it should display CLK_TCK, the system value for how many
clock ticks or operations there SHOULD be per second, and a formula
must be put in place of "clocks_per_sec" to calculate the following:
 how many instructions the inner operation takes, times its current
number of loops (k), divided by number of seconds that have elapsed.

Note that this will not work on newer computers for at least a few
reasons: they're too fast, and k needs to be longer than a "long"
integer to work.
clock ticks cannot be obtained on many newer systems via CLK_TCK
(system returns 0).
And, some processors "pipeline" instructions, excecuting multiple
commands in a single clocktick, or optimize looped commands that they
can "forsee" by looking ahead at the code.

Hope this answers your question.

. bookface-ga
Comments  
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 answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


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