Consider:
#include <time.h>
#include <windows.h>
clock_t tStart = clock();
for (int i = 0; i < 10; i++) {
Sleep(10);
}
printf("Time taken: %.3fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
Time taken: 0.102 s (varied)
clock_t tStart = clock();
for (int i = 0; i < 10; i++) {
doSomething();
Sleep(10);
}
printf("Time taken: %.3fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
Time taken: 0.105 s (varied)
How can I adjust my Sleep() so that the program consistently ends on printing 0.100 s each time? (and that doSomething(); executes exactly every 0.010s). Is that even possible?
So far I have tried to calculate the time each iteration of the loop takes and decrease the following Sleep() by that amount, but to no avail. Additionally, if making something execute every 0.010 s is not possible, what is the minimum time I can consistently make something execute at?