| | |
Timer Function?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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
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
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:
Can anyone give me some suggestions? Thanks
C Syntax (Toggle Plain Text)
for(conditions) int x, y, timetaken; x = (get the time for right now); ...more condions... y = (get the time for right now); timetaken= y-x; ...rest of code...
Can anyone give me some suggestions? Thanks
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
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.
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <time.h> int main () { time_t start, endtime; unsigned long choice; unsigned long t; int difference; start = time(NULL); printf ("%ld seconds have passed since January 1, " "1970(Otherwise known as the Epoch)\n\nPlease enter the " "number to loop to: ", start); scanf("%ld", &choice); for(t = 0; t<choice; t++) endtime=time(NULL); difference = difftime(endtime, start); printf("\n\nThe count to %ld took %d seconds!",t, difference); return 0; }
![]() |
Similar Threads
- Visual Basic Timer/Clock/Countdown. (Visual Basic 4 / 5 / 6)
- ASP.Net timer control (ASP.NET)
- Descending timer problem (C#)
- Please Help me with VB Timer (Visual Basic 4 / 5 / 6)
- timer function and scanf (C)
- How to pause a timer (C++)
- Problem with timer (urgent) (Visual Basic 4 / 5 / 6)
- Timer code pls using second counts (Visual Basic 4 / 5 / 6)
- timer urgent (ASP.NET)
- timer starts from 00:00:00 (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Format of the number
- Next Thread: Create multiple trees using struct
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking hardware histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi





