Thread: Calculate Execution Time?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53

    Calculate Execution Time?

    How to calculate Execution time in milliseconds?
    if i use time() function i can get the result only in seconds .
    What about clock function ? How to print the difference using clock_t variable?

    ts=clock();
    quicksort(x,0,size-1);
    printf("Time elapsed: %f\n", ((double)clock() - ts) / CLOCKS_PER_SEC);

    its only resulting in Seconds .
    Last edited by infantheartlyje; 10-14-2011 at 12:57 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you're on Windows...
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main(void)
      { long long int Elapsed;
         
        Elapsed = GetTickCount();
    
    
         // do something really long here...
    
    
        printf("That took %lld milliseconds", GetTickCount() - Elapsed);
    
        return 0;
    }
    Now if you're not on Windows...
    Code:
    // ?????

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by CommonTater View Post
    Now if you're not on Windows...
    Yes. Its not windows programming. Any way thank you.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about telling us what it is (reducing the space to 1), rather than what it isn't (which merely reduces N to N-1).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by infantheartlyje View Post
    Yes. Its not windows programming. Any way thank you.
    On all *nix systems, you can use gettimeofday();
    With that you can get to upto nanoseconds accuracy.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Do you want the wall-clock time or the actual time that the processor is running your code?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by manasij7479 View Post
    On all *nix systems, you can use gettimeofday();
    With that you can get to upto nanoseconds accuracy.
    Very helpful. Thanks a lot !!!

  8. #8
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by iceaway View Post
    Do you want the wall-clock time or the actual time that the processor is running your code?
    Actual time that the processor is running.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by infantheartlyje View Post
    Actual time that the processor is running.
    Then you should probably use clock() instead of gettimeofday().

    Code:
    clock_t t0, t1, elapsed;
    t0 = clock();
    /* Call function */
    t1 = clock();
    elapsed = 1000 * (t1 - t0) / (CLOCKS_PER_SEC);
    printf("Avg elapsed time: %ld ms\n\n", elapsed);
    Last edited by iceaway; 10-14-2011 at 02:15 AM.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by iceaway View Post
    Then you should probably use clock() instead of gettimeofday().
    But by using this clock() function i couldn't get the milliseconds ?

  11. #11
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by infantheartlyje View Post
    But by using this clock() function i couldn't get the milliseconds ?
    If you read the man page I linked to, you would see that dividing the return value from clock() with CLOCKS_PER_SEC gives you the result in seconds. Hence, if you multiply (t1 - t0) by 1000 before you divide by CLOCKS_PER_SEC you get the result in milliseconds. Or you can divide (t1 - t0) by (CLOCKS_PER_SEC/1000) if you expect very long times which could cause wraparounds.

    Also read the "Conforming to" and see if any of that applies to you.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is what I've used:
    Code:
    //add:
    #include <time.h>
    
    //and in your program:
    clock_t start, stop;
    
    start = clock();
    //rest of the program here
    
    stop = clock();
     printf("\n         Elapsed Time:     %.3lf seconds\n", (double)(stop-start)/CLOCKS_PER_SEC);
    Works in Windows with Pelles C. Standard C though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to calculate system time
    By aznbottle in forum C Programming
    Replies: 1
    Last Post: 12-09-2010, 12:30 AM
  2. Calculate time
    By Goo in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2009, 05:24 PM
  3. execution time
    By shuo in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2007, 02:58 AM
  4. Calculate time left...
    By IndioDoido in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 05:33 PM
  5. calculate elapsed-time
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-26-2001, 07:42 AM

Tags for this Thread