clock()

This is a discussion on clock() within the C Programming forums, part of the General Programming Boards category; Hi I got some problem with the clock fct. Code: #include <time.h> #include <stdio.h> int main(int argc, char *argv[]) { ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    clock()

    Hi I got some problem with the clock fct.

    Code:
    #include <time.h>
     #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
          clock_t start, end;
          double cpu_time_used;
     
          start = clock();
    
           	int i = 0;
    	for(i= 0;i++;i<500);
    	
          end = clock();
          cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    
    	printf("start: %e\n", start);
    	printf("end: %e\n",end);
            
    }
    
    }
    Why do I get start and end to be equal?


    And if I add printf("end: %e\n",cpu_time_used); in the end I get start, end and cpu_time_used all equal to zero.
    I don't get it! *smash head on the wall*

    In the GNU system, `clock_t' is equivalent to `long int'
    Info: (libc.info.gz) Processor And CPU Time
    But when I write

    Code:
    printf("start: %ld\n", start);
    I get start = 0;
    Why is that?
    Last edited by jordanguyoflove; 03-26-2009 at 07:59 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    /CLK_TCK;

    Or whatever your compiler supports. Basically, the number of ticks per second, on your system, for this timer.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A clock-tick is often 10ms, so anything shorter than that will register as "same".

    Chances are also that the compiler completely removes the loop altogether, since it doesn't actually produce anything!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Quote Originally Posted by Adak View Post
    /CLK_TCK;

    Or whatever your compiler supports. Basically, the number of ticks per second, on your system, for this timer.

    You have to use CLOCKS_PER_SEC instead of CLK_TCK. According to
    /usr/include/time.h, CLK_TCK is the "obsolete POSIX.1-1988 name"
    for CLOCKS_PER_SEC.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Quote Originally Posted by matsp View Post
    A clock-tick is often 10ms, so anything shorter than that will register as "same".

    Chances are also that the compiler completely removes the loop altogether, since it doesn't actually produce anything!

    --
    Mats

    Code:
    #include <time.h>
     #include <stdio.h>
    #include <sys/time.h>
    
    
    int main(int argc, char *argv[])
    {
          clock_t start, end;
          double cpu_time_used;
     
          start = clock();
    
           	int i = 0;
    	int u = 0;
    	int y = 0;
    	int z = 0;
    
    	for(i= 0;i++;i<5000000)
    	{
    		for(u= 0;u++;u<5000000)
    		{
    		
    			for(i= 0;i++;i<500000000)
    			{
    				z = z +1;
    				if(z > 1000000)
    				 z = 0;	
    
    			}
    		
    
    		}
    
    	}
    	
          end = clock();
          cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    	//cpu_time_used = ((double) (end - start)) /CLK_TCK;
    	//printf("start: %e\n", start);
    	//printf("end: %e\n",end);
              printf("end: %f\n",cpu_time_used); 
    }
    cpu_time_used = 0.

    ME ........ING ANGRY!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try printing z after your loop!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    the middle of nowhere
    Posts
    11
    just a fyi. your int main needs a return statement

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So put a delay(500) inside the code you're timing, and knock 1/2 second off the final time it reports.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Quote Originally Posted by matsp View Post
    Try printing z after your loop!

    --
    Mats

    Thanks.

    I corrected it.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Quote Originally Posted by CrustyClown View Post
    just a fyi. your int main needs a return statement
    Thanks

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Code:
    #include <time.h>
     #include <stdio.h>
    #include <sys/time.h>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     int
          timeval_subtract (result, x, y)
               struct timeval *result, *x, *y;
          {
            /* Perform the carry for the later subtraction by updating Y. */
            if (x->tv_usec < y->tv_usec) {
              int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
              y->tv_usec -= 1000000 * nsec;
              y->tv_sec += nsec;
            }
            if (x->tv_usec - y->tv_usec > 1000000) {
              int nsec = (x->tv_usec - y->tv_usec) / 1000000;
              y->tv_usec += 1000000 * nsec;
              y->tv_sec -= nsec;
            }
     
            /* Compute the time remaining to wait.
               `tv_usec' is certainly positive. */
            result->tv_sec = x->tv_sec - y->tv_sec;
            result->tv_usec = x->tv_usec - y->tv_usec;
     
            /* Return 1 if result is negative. */
            return x->tv_sec < y->tv_sec;
          }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    int main(int argc, char *argv[])
    {
          clock_t start, end;
          double cpu_time_used;
    	struct timespec echodelay;
    
    
          start = clock();
    	
           	int i = 0;
    	int z = 0;
    	
    
    	for(i= 0;i<1000000000;i++)
    	{
    		z++;
    		if (z > 1000)
    			z = 0;
    
    	}
    	
          end = clock();
          cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    	//cpu_time_used = ((double) (end - start)) /CLK_TCK;
    	  printf("start: %e\n", start);
    	  printf("end: %e\n",end);
    
    
    	timeval_subtract (&echodelay, &end, &start);
    		
    	    int sec  = echodelay.tv_sec;
    	    int nsec = echodelay.tv_nsec;
    	    
    	   printf("Echo delay is %ds and %d ns\n", sec, echodelay.tv_nsec);
              printf("CPU: %f\n",cpu_time_used);
    		
     
    return 0;
    
    }
    At last I got it.

    Echo delay is 4551208s and 478712 ns



    This is 4.551208478712 sec?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are printing rubbish. clock_t is not a timeval structure - clock_t is an integer (32-bit, most likely). Since you are passing the address of it, whatever comes AFTER your start/end variables will be taken as part of the timeval - and of course, that gives you some randomly curios value, not the actual difference between two clock() calls.

    Also:
    Code:
     int
          timeval_subtract (result, x, y)
               struct timeval *result, *x, *y;
    is OLD style C - newer (since 1989 at least) uses type declaration inside the parenthesis.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Rubbish...darn!

    But it really took 4.5s.

    Jebus!

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think the problem was your for loops:
    Code:
    for(i= 0;i++;i<5000000)
    which you fixed in your last version (you had the condition & increment statements mixed up).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

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, 01: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, 06:15 AM
  4. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 11:12 PM
  5. using clock()
    By sl4nted in forum C Programming
    Replies: 8
    Last Post: 11-09-2006, 07:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21