timing my program

This is a discussion on timing my program within the C Programming forums, part of the General Programming Boards category; how do i time my program? i googled the topic but all teh results were ANSI C and they didnt ...

  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, 08:38 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01: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, 11:04 PM

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