Results 1 to 4 of 4
So, I was having some trouble with clock(). Here's some code that fails:
Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
printf("%d\n", clock());
sleep(2);
printf("%d\n", clock());
return 0;
...
- 03-06-2009 #1incripshinGuest
SOLVED clock() always gives 0
So, I was having some trouble with clock(). Here's some code that fails:
It prints:Code:#include <stdio.h> #include <time.h> #include <unistd.h> int main() { printf("%d\n", clock()); sleep(2); printf("%d\n", clock()); return 0; }
0
0
Any ideas? I tried disabling the 'tickless clock' option in the kernel. As far as clock(3) is concerned, this is never supposed to happen.
- 03-06-2009 #2
Hmm,
clock_t isn't said to be integer. Only the return of CLOCKS_PER_SEC has to be int.Debian GNU/Linux -- You know you want it.
- 03-06-2009 #3incripshinGuest
Casting it to doubles produces 0.0000 and 0.0000. I looked it up and it's defined on my system to be a signed long int.
- 03-06-2009 #4incripshinGuest
Okay, so the code I was initially trying to time was supposed to be very complex, but was being skipped over. Also, sleep() won't increase the value returned by clock(). clock() only increases if something useful is being done. So, all is well.


Reply With Quote