Find the answer to your Linux question:
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; ...
  1. #1
    incripshin
    Guest

    SOLVED clock() always gives 0

    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;
    }
    It prints:
    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.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    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.

  3. #3
    incripshin
    Guest
    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.

  4. #4
    incripshin
    Guest
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...