time.h

This is a discussion on time.h within the C Programming forums, part of the General Programming Boards category; Can someone give me a quick example of using time.h to time how long something runs for? i would like ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    time.h

    Can someone give me a quick example of using time.h to time how long something runs for?

    i would like to output in human readable time.
    I was doing this:
    Code:
    clock_t start = clock();
    
        clock_t end = clock();
       
        printf("\t Time : %d \n", (end - start)/CLOCKS_PER_SEC);
    but it is not giving me what i want.

    Thanks

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Start and end are going to have the same values, since theres not really a pause between them.

    So no matter, Time is always going to be 0 (regardless of what CLOCKS_PER_SEC is).

    (end-start) = 0 (probably), And Say CLOCKS_PER_SEC = 1000, 0/1000 is 0...

    Understand?

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by zensatori
    Can someone give me a quick example of using time.h to time how long something runs for?
    You mean like this one in the faq?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Quote Originally Posted by zacs7 View Post
    Start and end are going to have the same values, since theres not really a pause between them.

    So no matter, Time is always going to be 0 (regardless of what CLOCKS_PER_SEC is).

    (end-start) = 0 (probably), And Say CLOCKS_PER_SEC = 1000, 0/1000 is 0...

    Understand?
    yeah, i know. I was just showing the syntax i am using. It is not mean as a exaple of my implimentation. Sorry i didn't make that clear and thanks for the suggestion.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i tried the example in the FAQ and its always giving me 0 seconds as the interval (if theres a 1sec pause or if theres a 5sec pause), how come?

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Quote Originally Posted by Ken Fitlike View Post
    You mean like this one in the faq?
    Yes... Thanks! I feel like a real n00b now.
    edit: I just changed my output to printf to %f and I get 0.00000 also, same as the guy above. Suggestions?
    edit edit: Never mind, i got it.
    Last edited by zensatori; 04-13-2007 at 09:29 PM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    does it work for you zensatori?

    edit: i didnt change anything from the FAQ, straight copy-paste and its always giving me 0. start and end are always 0. i havent seen anything about it that its OS-dependant, but im on linux with 2.6 kernel.
    Last edited by nadroj; 04-13-2007 at 09:30 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I also get zero as my result compiling under cygwin, using gcc 3.3.4. My start and stop values are usually a consistent pair of values, but the number itself varies with each execution.


    Quzah.
    Last edited by quzah; 04-13-2007 at 09:36 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    I have better results if I use %g instead of %f. Apparently float is not precise enough.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It should be on mine. clock however doesn't seem to be actually doing anything. I'm running an edit now to see if I can get the two calls to clock to give me different results. They're both 15 for me on last run, and CLOCKS_PER_SEC is only 1000, so a float should be fine. It's just that 15/15 = 0.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    for me start is always 0, and end is variable.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    50000 numbers read.
    Bubble sort ... OK
    Time : 16.460000
    Insertion sort ... OK
    Time : 5.260000
    Heap sort ... OK
    Time : 0.020000

    Done! Time : 36.630000
    I get an output like this when i time some functions

    I will try %g and see what i get.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    15/15 = 0
    is 15/15 not 1 ?
    Last edited by zacs7; 04-13-2007 at 10:18 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zacs7 View Post
    is 15/15 not 1 ?
    Not when you're this tired it's not.

    [edit]
    Ah, it finally finished, and I finally got something other than 0.
    Code:
    #include <stdio.h> 
    #include <time.h> 
    
    int main ( void )
    {
      clock_t start, end;
      unsigned int x=1;
    
      /* Start the timer */
      start = clock();
    
      while( x++ )
          putchar( '.' );
    
      printf ( "Please wait a few moments and hit return\n" );
      getchar();
    
      /* End the timer */
      end = clock();
      
      /* Print out the difference */
      printf ( "The interval was: &#37;f seconds\n",
        (double)(( end - start ) / (double)CLOCKS_PER_SEC ) );
      printf ( "start %d, end %d, CLOCKS_PER_SEC %d\n", start, end, CLOCKS_PER_SEC );
        
      return 0;
    }
    
    /*
        My output:
    
    ....four billion.....Please wait a few moments and hit return
    
    The interval was 395.422000 seconds
    start 15, end 395437, CLOCKS_PER_SEC 1000
    */
    Which is clearly wrong, because I drove to the store while it was running for ~45 minutes or more.

    [/edit]

    Quzah.
    Last edited by quzah; 04-13-2007 at 11:02 PM.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    51
    I'm using Linux Mandrake 10.1 and ran the FAQ program.
    The result too was 0.0000.
    And my CLOCKS_PER_SECOND is 1000000...

    At first start & end is always 0.
    After running it for about 20minutes+ ,
    start =0 ; end = 5.002045 (stuck at this value now for more recent executions too)...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time.h ctime and milliseconds
    By Deewhyandy1 in forum C Programming
    Replies: 4
    Last Post: 01-26-2008, 12:42 AM
  2. Help with time.h functions please.
    By Ifurita. in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2003, 03:58 AM
  3. time.h and miliseconds question
    By Diamonds in forum C++ Programming
    Replies: 10
    Last Post: 12-16-2002, 07:41 AM
  4. Replies: 2
    Last Post: 10-18-2002, 08:30 AM
  5. a time.h problem
    By Max in forum C Programming
    Replies: 14
    Last Post: 10-15-2002, 02:43 PM

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