Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

c++ - The C `clock()` function just returns a zero

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?

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

int main()
{
    clock_t start, end;
    double cpu_time_used;

    char s[32];

    start = clock();

    printf("
Sleeping 3 seconds...

");
    sleep(3);

    end = clock();

    cpu_time_used = ((double)(end - start)) / ((double)CLOCKS_PER_SEC);

    printf("start = %.20f
end   = %.20f
", start, end);
    printf("delta = %.20f
", ((double) (end - start)));
    printf("cpu_time_used  = %.15f
", cpu_time_used);
    printf("CLOCKS_PER_SEC = %i

", CLOCKS_PER_SEC);

    return 0;
}
Sleeping 3 seconds...

start = 0.00000000000000000000
end   = 0.00000000000000000000
delta = 0.00000000000000000000
cpu_time_used  = 0.000000000000000
CLOCKS_PER_SEC = 1000000

Platform: Intel 32 bit, RedHat Linux, gcc 3.4.6

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

clock() reports CPU time used. sleep() doesn't use any CPU time. So your result is probably exactly correct, just not what you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...