Originally Posted by
matsp
There is nothing wrong with the original code posted (it would show the number of clock-ticks, so dividing by CLOCKS_PER_SEC would give the time in seconds).
The problem with the time being zero is either that the time of that loop is so short that it's not registering even one tick - make the loop longer. But it may well be that the compiler removes the empty loop as it's not actually doing anything.
--
Mats
Code:
#include <time.h>
#include <stdio.h>
#include <windows.h>
int main(void)
{
clock_t ticks1, ticks2;
Sleep(265);
ticks1 = clock();
ticks2 = ticks1;
while((ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC)<1)
ticks2=clock();
printf("Took %ld ticks to wait one second. ticks2 = %ld, ticks1 = %ld\n",ticks2 - ticks1, ticks2, ticks1);
printf("This value should be the same as CLOCKS_PER_SEC which is %ld.\n",CLOCKS_PER_SEC);
return 0;
}
That code is weird, notice the windows Sleep function I put there. When I sleep for 265 miliseconds, ticks1 will be 265, so will ticks2 (lines 10 and 11). This is my output:
Code:
Took 735 ticks to wait one second. ticks2 = 1000, ticks1 = 265
This value should be the same as CLOCKS_PER_SEC which is 1000.
Apparently, the loop breaks when ticks2 is 1000, and it took 735 ticks to wait one second, which is not true because CLOCKS_PER_SEC is 1000 in my computer.
Now, if I do it the way I put it, it works. And if I remove the Sleep() function, it also works. But If the program had some time running, then it doesn't work.