-1

I think the output of following code should be excatly or near the macro CLOCKS_PER_SEC, however, the output is not very consistant.

int main(int argc, char const *argv[])
{
    clock_t before = clock();

    while (1) {
        sleep(1);
        printf("Res: %ld\n", clock() - before);
        fflush(stdout);
        before = clock();
    }
}

Sample Output:

Res: 0
Res: 1301
Res: 1540
Res: 1597
Res: 1631
Res: 1689
Res: 1740
Res: 1783
Res: 1835
Res: 1902
Res: 1956
Res: 1981
Res: 2045
Res: 2098
Res: 2147
Res: 2207
Res: 2265
Res: 2323

I assume the clock() function only return the excution time of the code.

So, How can I measure real world time in c?

user762750
  • 159
  • 7
  • 1
    Don't expect `sleep` to give you exactly yhe time you ask for... – Support Ukraine Sep 15 '19 at 05:04
  • 1
    `clock()` does not give you a wall clock time, so that's definitely the wrong thing to use. The normal way to get the wall clock time is to use `time()`, but that gives a precision only in seconds. There is no portable way to have greater precision; you will need to use a platform-dependent function. – jamesdlin Sep 15 '19 at 05:08
  • @jamesdlin So, is there any platform specific way to do it? – user762750 Sep 15 '19 at 05:09
  • 1
    @user762750 There exist platform specific ways to do it (e.g. [GetSystemTime](https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtime) on Windows). Is there a way to do it for the platform you're targeting? I don't know since you haven't said what that platform is. – jamesdlin Sep 15 '19 at 05:10
  • @jamesdlin Linux – user762750 Sep 15 '19 at 05:14
  • You can't do that as a program in a multitask system, your program is sharing the CPU with other programs and it will only register the time when it is it's turn in the CPU again and it notices the sleep time has elapsed. – Havenard Sep 15 '19 at 05:14
  • "the clock() function only return the excution time of the code." It should in theory, but in practice it depends on your platform. – n. m. could be an AI Sep 15 '19 at 05:14
  • @Havenard So, there is no way I can do it in linux or windows? – user762750 Sep 15 '19 at 05:18
  • Maybe if you write a driver you can use interrupts to trigger events at exact time intervals, but an ordinary application cannot do that. Also your code calls `fflush(stdout)` that basically tells the program to halt until the output flushes, this can easily be seen as a `sleep(rand())` since your program doesn't know how long that operation is going to take. – Havenard Sep 15 '19 at 05:20
  • @Havenard It's not clear what "that" is when you say "You can't do that...". I'm pretty sure *you* mean that you can't expect `sleep(1)` to take *exactly* 1 second. The actual question (at least as given by the title), however, is how to measure a wall clock time interval in milliseconds (and accounting for things like `sleep()`), and that *can* be done. – jamesdlin Sep 15 '19 at 05:24
  • @jamesdlin Yeah I'm basing my comment on the program he wrote, apparently this is just another "why is my `sleep()` not doing exactly as specified" sort of question. – Havenard Sep 15 '19 at 05:26
  • If you are looking for Fixed-Time Iterations, you might find [timer struct for fixed timestep iterations in C (gcc)](https://codereview.stackexchange.com/questions/223405/timer-struct-for-fixed-timestep-iterations-in-c-gcc) useful. – David C. Rankin Sep 15 '19 at 05:54

2 Answers2

4

From the documentation:

The value returned is the CPU time used so far as a clock_t; to get the number of seconds used, divide by CLOCKS_PER_SEC. If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t) -1.

So the output shows the time the processor was used. Since sleep does not do a busy wait, much less than a second is used on the processor.

For getting real elapsed time see for example How do I measure a time interval in C?

Henry
  • 42,982
  • 7
  • 68
  • 84
0

There is no way to measure elapsed time with better than one second resolution in the C standard.

One popular non-standard function is gettimeofday from POSIX. It is good for obtaining current wall clock time with 1 ms resolution. It does not come standard on Windows but free implementations for that platform are easy to find.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243