Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clock() and TImes() problem!!! (URGENT)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi ,
I have the following simple program.

================
#include<time.h>
clock_t start,finish;

start=clock();

sleep(10);

finish=clock();
Printf(&quot;\n start= %f finish=%f &quot;,start,finish);

===============================

Output: start=0.0 finish=0.0

=================================
The clock function is returning 0 always. I couldn't able to find the reason. Please help me out......... Same thing I am facing with times().

Thanks in advance,
Venu
 
Then, what is Printf? Is it redefined printf?
Try again using 'printf' instead of 'Printf'.

I tested your example and it works well for
me when printf is used and casting is applied
to 'start' and 'finish'.

Thanks.
Hee S. Chung
heesc@netian.com
 
Hi Chung,
Thanks for your quick response

I didn't define printf .Even I used &quot;times&quot; function also but no change.
I think it is system dependent. Here I am working on HP-UX10.0.

Even I tried in C++ also(with cout<<start<<finish<<endl) but no change.
Does it require seperate compile options??

Thanks,
Venu
 
The standard c function printf is not Printf.
So I thought you redefined Printf instead of printf.
But what is Printf you used?

I don't think it is option or system dependent.
Then pleast test following things :

1. %u or %lu instead of %f without casting for
'start' and 'finish'
printf( &quot;%u %u\n&quot;, start, finish );

2. %f test
printf( &quot;%f&quot;, 1.5 );
Hee S. Chung
heesc@netian.com
 
time_t is not always a float.

For example, in the GNU C library, time_t is equivalent to long int. In other systems, time_t might be either an integer or floating-point type.

Try printing with %ld format specifier. Also try %lf for double.
 
put %d or %ld in the printf.


I think the problem is not in the printf statement. Its actually in the sleep (10);

sleep, stops the total process including the clock ticks. So the starting and ending clock ticks are zero in your case. So put some processing statements between the start and end clocks like the following and check the result now.

start=clock();
//sleep(0);
for(i = 99999; i > 0; i--)
for(j = 999; j > 0; j--);
finish=clock();

Maniraja S
 
Hi ,
Thanks a lot for quick response. yes, &quot;sleep&quot; stoping the clock ticks, I tested with above example.

Once again thanks to all for helping..

Venu

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top