time question.

This is a discussion on time question. within the C Programming forums, part of the General Programming Boards category; is there any "standard" code to measure how long it takes for a program to run? or can someone give ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    time question.

    is there any "standard" code to measure how long it takes for a program to run? or can someone give me an example of how to time my program?
    When no one helps you out. Call google();

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Store the value of clock() at the start of your program, and then take the value of clock() at the end of your program. The difference divided by CLOCKS_PER_SEC is the time it took for your code to run in seconds.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The only standard method I know of only has accuracy to the second. I would recommend you do a board search as this has come up fairly reguarly and has been discussed quite in depth.

    If seconds precision is enough you can use the time() function to acquire the time at the beginning of the program, then again at the end, subtract the difference and you have the amount of time it took to run.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    what if i want to time how fast a function gives me a result? vs another function that uses a different algorithm or does some extra work
    When no one helps you out. Call google();

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Since both functions probably execute quite quickly, your best bet is to time how long it takes to run each function 1000 times, and compare those values. Just put the clock() statements before and after the loop.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    Also consider profiling your code. That's exactly what profilers are ther to do for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 03:44 AM
  3. It's a question of time ...
    By Mandy_C in forum C Programming
    Replies: 4
    Last Post: 04-09-2006, 02:18 PM
  4. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  5. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 03:33 AM

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