Thread: timing my program

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    timing my program

    how do i time my program?

    i googled the topic but all teh results were ANSI C and they didnt work.


    On a side note where do you get information on C, i can only find C++ and C#. MSDN doesnt even have C anymore.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    What do you mean time your program? Like make it close after a certain amount of time?

    You will probably have to use the time.h header, then compare the current time with the time limit in a loop and close the program if the limit is exceeded.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I expect you mean time it for example to do performance comparisons, in which case this model will work:

    Code:
    #include <clock.h>
    
    int main(void)
    {
        clock_t t1, t2;
    
        t1 = clock();
        ....
        // do something that takes some time 
        .... 
        t2 = clock();
        printf("Time taken: %.2f\n", (double)(t1 - t2) / CLOCKS_PER_SEC);
    }
    It's reasonably portable and should work for anything that takes more than a few hundreds of a second - for really short times you'll need something more accurate.

    --
    Mats

  4. #4
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    that worked perfectly thanks so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timing program
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 08-13-2007, 07:38 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM