I tried this, but it doesn't work well, it shows a number like 0.000053
begin = clock();
scanf(" %c", &in);
end = clock();
printf("%f\n", (double)(end - begin) / CLOCKS_PER_SEC);
I tried this, but it doesn't work well, it shows a number like 0.000053
begin = clock();
scanf(" %c", &in);
end = clock();
printf("%f\n", (double)(end - begin) / CLOCKS_PER_SEC);
clock
measures "CPU time," not "wall-clock time." The difference is (approximately) that CPU time does not include time spent waiting for something to happen. 0.000053 seconds (53 microseconds) is in the right ballpark for the amount of CPU time consumed by this use of scanf
-- no matter how long you waited before you pressed Enter.
time
, gettimeofday
, and clock_gettime(CLOCK_MONOTONIC)
(in increasing order of precision) measure wall-clock time. If you need to measure wall-clock time, use one of those.