Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-30-2006, 09:40 AM   #1 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default timing in C using clock()

I am writing an application in C that requires timing between events. Now looking through <time.h> it seems that calling clock() will return the number of clock ticks of elapsed processor time. However whatever i seem to do the result is always 0.

It wouldn't be feasible to post all of my code here, but this is a small routine i wrote which shows the problem. Hopefully it is just me doing something stupid and can be fixed!

The code below just asks the user for inputs until you give it an EOF caracter (ctrl^D). The start time is always zero and for some reason, unknown to me, the end time is also always ends at zero no matter how long you leave the program running.

The code:
Code:
#include <time.h>
#include <stdio.h>

int main(){
	clock_t start, end;
	double elapsed;
	int input;
	
	start = clock();

	while(scanf("%d\n",&input)!=EOF);

	end = clock();
	elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;

	printf("\nstart = %ld",start);
	printf("\nend = %ld",end);
	printf("\nelapsed time = %e\n",elapsed);

	return 0;
}
Compiles with "gcc -ansi -pedantic -Wall <filename>.c"

The output
Code:
$ ./a.out

start = 0
end = 0
elapsed time = 0.000000e+00

Hoepfully someone can shed some light on what is going on here or is there another way of accurately timing the length of time something takes to execute in c

thanks
fitzjj is offline  
Old 03-30-2006, 10:58 AM   #2 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

re-reading my post may have shed some light on what the issue might be:
Quote:
calling clock() will return the number of clock ticks of elapsed processor time
Now obviously sitting waiting for input from a user used no processor time hence why the clock is not going up. Sitting in a for loop does use processor time and so when i replace the while(scanf... bit with a for loop the clock goes up.

I believe i should instead be using the type 'time_t' and function 'time(NULL)'

does this sound about right?
fitzjj is offline  
Old 03-30-2006, 11:45 AM   #3 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

Ok i think i have this one wrapped up. I'll post a solution in case someone stumbles across this post with a similar probalem to what i had:

Code:
#include <time.h>
#include <stdio.h>
#include <unistd.h>

int main(){

	int i;
	time_t start, end;
	
	start=time(NULL);
	
	for(i=0;i<10;i++){
		printf("\n> %ld",time(NULL));
		sleep(2);
	}
	
	end=time(NULL);
	
	printf("\napplication ran for a duration of %ld seconds\n",end-start);	

	return 0;
}
Basically the code above prints the time 10 times at intervals of 2 seconds. It then works out how long the app ran for, which is obviously 2*10=20seconds.
fitzjj is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On