The C `clock()` function just returns a zero

前端 未结 6 808
醉话见心
醉话见心 2020-11-30 07:58

The C clock() function just returns me a zero. I tried using different types, with no improvement... Is this a good way to measure time with good precision?

6条回答
  •  星月不相逢
    2020-11-30 08:19

    Calling sleep() isn't going to use up any CPU time. You should see a little difference, though. I corrected your printf type mismatch bug in this line:

    printf("start = %.20f\nend   = %.20f\n", start, end);
    

    And it gave reasonable results on my machine:

    start = 1419
    end   = 1485
    delta = 66
    cpu_time_used  = 0.000066000000000
    CLOCKS_PER_SEC = 1000000
    

    You might try gettimeofday() to get the real time spent running your program.

提交回复
热议问题