Timer Function?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 11
Reputation: Erikmmp is an unknown quantity at this point 
Solved Threads: 0
Erikmmp's Avatar
Erikmmp Erikmmp is offline Offline
Newbie Poster

Timer Function?

 
0
  #1
Dec 4th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: Erikmmp is an unknown quantity at this point 
Solved Threads: 0
Erikmmp's Avatar
Erikmmp Erikmmp is offline Offline
Newbie Poster

Re: Timer Function?

 
0
  #2
Dec 4th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 966
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is online now Online
Posting Shark

Re: Timer Function?

 
0
  #3
Dec 4th, 2008
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Timer Function?

 
0
  #4
Dec 5th, 2008
Here's a useful link
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: Erikmmp is an unknown quantity at this point 
Solved Threads: 0
Erikmmp's Avatar
Erikmmp Erikmmp is offline Offline
Newbie Poster

Re: Timer Function?

 
0
  #5
Dec 6th, 2008
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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC