Like Tree1Likes

Measuring Run Time

This is a discussion on Measuring Run Time within the C Programming forums, part of the General Programming Boards category; Currently I'm studying from 'The C Programming Language' and it asks you to modify some code and then compare the ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55

    Measuring Run Time

    Currently I'm studying from 'The C Programming Language' and it asks you to modify some code and then compare the run times. What is the best way to do this?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <windows.h>
    
    unsigned long int Time = GetTickCount();
    
    
    
    // do whatever it is in here
    
    
    
    printf("That took %ld milliseconds", GetTickCount() - Time);
    Last edited by CommonTater; 09-12-2011 at 01:58 PM.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    Thanks for the reply. Any idea how I could do it on os x? :P

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BIOS View Post
    Thanks for the reply. Any idea how I could do it on os x? :P
    I'm sure they have equivalent functions... Google is your friend.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Can be pretty accurate.
    gettimeofday

    Portable.
    clock

    Anything sub-second needs a bit of care to make sure it is done well.
    The accuracy and precision of any fast clock you can get at can vary from one machine to another.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Can be pretty accurate.
    gettimeofday

    Portable.
    clock

    Anything sub-second needs a bit of care to make sure it is done well.
    The accuracy and precision of any fast clock you can get at can vary from one machine to another.
    Heck they can vary within the same machine... depending what else is running. But as a rule they're good enough for non-critical functions.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    Quote Originally Posted by CommonTater View Post
    Google is your friend.
    Had a feeling you'd say this! :P I did actually google first but figured asking would be quicker than trawling.

    @salem Thanks. Clock seems perfect

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    Just in case anyone else digs up this thread in future. Here is some example code showing how to use clock():

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
     
    int main()
    {
      clock_t start = clock();
      for (long i = 0; i < 100000000; ++i)
        exp(log((double)i));
      clock_t finish = clock();
     
      printf("It took %d seconds to execute the for loop.\n",
      (finish - start) / CLOCKS_PER_SEC);
     
      return 0;
    }
    outputs:

    Code:
    It took 23 seconds to execute the for loop.
    Adak likes this.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    +1 for using the standard clock() function. Good job, BIOS.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    327
    If you're trying to measure something short, like a single statement, you should subtract off the
    loop overhead, as it can be a significant part of the execution time.

    Just run the same time measurement on the the same loop empty.

    Then subtract that off the loop with the code under test in it.

    And might as well convert the result to time per single pass also.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If the loop is empty, your compiler may optimize it out, throwing the loop out completely.


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

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    If the loop is empty, your compiler may optimize it out, throwing the loop out completely.
    Quzah.
    Also... compared to the time for 100 million floating point calculations, the loop overhead is probably trivial...

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BIOS View Post
    Just in case anyone else digs up this thread in future. Here is some example code showing how to use clock():

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
     
    int main()
    {
      clock_t start = clock();
      for (long i = 0; i < 100000000; ++i)
        exp(log((double)i));
      clock_t finish = clock();
     
      printf("It took %d seconds to execute the for loop.\n",
      (finish - start) / CLOCKS_PER_SEC);
     
      return 0;
    }
    outputs:

    Code:
    It took 23 seconds to execute the for loop.
    Next actually assign the result to something... a = exp(log((double) i)); ... Optimization may have run an empty loop...

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    327
    Well it does depend on the code under test.

    But I was also including the loop counter increment and test. That's also done 100 million times.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Also... compared to the time for 100 million floating point calculations, the loop overhead is probably trivial...
    Also, since you used Turbo C, you don't even have numbers as big as 100 million, so your loop didn't really run that many times.


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

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

Similar Threads

  1. measuring function execution time
    By nik in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2011, 08:21 PM
  2. measuring time in ms/us/ns
    By wanwan in forum C Programming
    Replies: 3
    Last Post: 04-29-2005, 01:42 PM
  3. measuring time of execution
    By KevBin in forum C Programming
    Replies: 6
    Last Post: 12-02-2004, 01:08 PM
  4. Measuring time
    By BruceLeroy in forum C++ Programming
    Replies: 20
    Last Post: 10-07-2004, 02:17 PM
  5. Measuring time of a reply
    By Zewu in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2003, 11:37 AM

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