Thread: help with clock()

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    14

    help with clock()

    Hi,
    I just want a program to be in an infinite loop but only do part of the code once a certain amount of time has elapsed.

    I've tried using clock() but can't get it to work. Is the clock reset everytime in a while() loop?

    Here's my code so far. I try to print the "duration" everytime through the loop but it's always 0.000000000.

    Code:
    #include <time.h>
    
    int i = 0;
    clock_t start, finish, current;
    double	duration;
    
    int main(){
    
    
    	start = clock();
    
    	while(1){
    	duration = (double) (current - start) / CLOCKS_PER_SEC;
    	printf("Time elapsed = %.10f\n", duration);
    	
    	if (duration >= 5){
    	printf("5 Seconds is UP\n");
    	}
    	}
    	return 0;
    	
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is similar.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. Clock Troubles
    By _Nate_ in forum C Programming
    Replies: 22
    Last Post: 06-19-2008, 05:15 AM
  4. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  5. System clock
    By bazzano in forum C Programming
    Replies: 10
    Last Post: 03-27-2007, 10:37 AM