Search code examples
cmacosclockdarwintime.h

time.h clock() broken on OS X?


Am I going mad? I'm running this on x86_64.

#include <stdio.h>
#include <time.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC);
    sleep(1);
    printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC);
    sleep(1);
    printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC);
    return 0;
}

This prints

Clock: 0.002880
Clock: 0.002968
Clock: 0.003019

It clearly waits for a second at the sleep(1) lines, but the output is clearly wrong.

If that doesn't work, is there a portable C alternative?


Solution

  • I'm an idiot. clock() returns processor time used.