What is the easiest way to do a milliseconds countdown in C (min:sec:milliseconds) and display it on screen ? I just need to execute the program into a cmd prompt window and see the seconds:milliseconds in order to measure precise time between screenshots.
Asked
Active
Viewed 536 times
1 Answers
1
#include <sys/time.h>
struct timeval ss1, ss2;
ss1.tv_usec; // first timestamp
gettimeofday(&ss1,NULL);
//Write code of screenshot capture
ss2.tv_usec; // second timestamp
gettimeofday(&ss2,NULL);
Note that tv_usec
will give you the time in microseconds and 1 millisecond = 1000 microseconds
.
Check this question's answers for other similar solutions.

Community
- 1
- 1

Chirag Bhatia - chirag64
- 4,430
- 3
- 26
- 35
-
sorry I didn't explain correctly, I just need to run the program into a cmd prompt window and the program should display the timer min:sec:milliseconds elapsed since the start of the program. So should I just do `printf("%d", ss1);` ? – Wicelo Jul 27 '14 at 18:52
-
`ss1.tv_usec` will give you the timestamp in microseconds. Once you calculate the difference between the two timestamps in microseconds, you can convert this result to min:sec:milliseconds by dividing the number accordingly, i.e. divide it by 60000000 to get minutes, divide the remainder of that by 1000000 to get seconds, and lastly divide the remainder of that by 1000 to get milliseconds. – Chirag Bhatia - chirag64 Jul 27 '14 at 18:59
-
I see so basically I need to do an infinite loop that runs every ms (or few ms) and display the counter. Do you know what I should use to stop the infinite loop for 1 or few ms ? – Wicelo Jul 27 '14 at 19:03
-
I'm not sure what your application is, so its hard to help with that. You could use an if condition to break out of the loop (maybe write this code in a separate function and return its value when the `condition` is true. – Chirag Bhatia - chirag64 Jul 27 '14 at 19:11
-
I want to use a pause function so my program don't use all CPU, is there a function like SDL_Delay() that takes a float ? – Wicelo Jul 27 '14 at 19:32
-
1You could use the `usleep()` function available in the `#include
` header. This function takes only int parameters in microseconds though and works only on Unix-based systems. To implement the same in Windows systems, you could use `Sleep()` (case-sensitive) which is part of `#include – Chirag Bhatia - chirag64 Jul 28 '14 at 03:51` directive which takes int parameters in milliseconds.