943,840 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 793
  • C RSS
Dec 4th, 2008
0

Timer Function?

Expand Post »
Hi everyone,
I am trying to make a program that times how long it runs for in seconds or milliseconds or whatever. The unit of time doesn't matter, I just need to know if C has any function I could use to do this. So, just as an example, I'm counting to 10,000 in a loop, and when it reaches 10,000 it stops and prints out the time it took to do. Would that be possible? Any links or snippets of code would be really appreciated. Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Erikmmp is offline Offline
11 posts
since Nov 2008
Dec 4th, 2008
0

Re: Timer Function?

This is just an additional thought but could I get the system time in seconds from the Epoch? So then I could do something like this:
  1. for(conditions)
  2. int x, y, timetaken;
  3. x = (get the time for right now);
  4. ...more condions...
  5. y = (get the time for right now);
  6. timetaken= y-x;
  7. ...rest of code...

Can anyone give me some suggestions? Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Erikmmp is offline Offline
11 posts
since Nov 2008
Dec 4th, 2008
0

Re: Timer Function?

Take the initial time with time() , use inside a loop take the time again, then use difftime() to check the diffrence between the two; if it's greater than the max time, break the loop.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Dec 5th, 2008
0

Re: Timer Function?

Here's a useful link
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Dec 6th, 2008
0

Re: Timer Function?

Thanks both of you for your help.
Here's the code I came up with. Its not perfect obviously but it lets the user pick a number to loop to and shows how long it takes to do that loop in seconds.
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int main ()
  5. {
  6. time_t start, endtime;
  7. unsigned long choice;
  8. unsigned long t;
  9. int difference;
  10. start = time(NULL);
  11. printf ("%ld seconds have passed since January 1, "
  12. "1970(Otherwise known as the Epoch)\n\nPlease enter the "
  13. "number to loop to: ", start);
  14. scanf("%ld", &choice);
  15. for(t = 0; t<choice; t++)
  16. endtime=time(NULL);
  17. difference = difftime(endtime, start);
  18. printf("\n\nThe count to %ld took %d seconds!",t,
  19. difference);
  20.  
  21. return 0;
  22. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Erikmmp is offline Offline
11 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Format of the number
Next Thread in C Forum Timeline: Create multiple trees using struct





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC