C17 Programming - measuring execution time, delaying program execution
Posted on April 17, 2019 by Paul
This is a short note about measuring the execution time for a C program, or delaying the program execution for a specified amount of time. I’m only interested in using mechanisms that let us measure the execution time with at least millisecond resolution, so I won’t mention the classical C way of getting the time of day with second resolution time or using the clock function.
You can find all the code examples at the GitHub repo for this article.
Since C11, the standard way to get a good time estimate is to use the timespec_get function, which returns the system clock time:
Where timspec will store the time split in seconds and nanoseconds.
Unfortunately, at the time of this writing, you can not use the timespec_get function on macOS. It works on Linux and Windows.
On Posix systems, like Linux and macOS, we can use the clock_gettime function to get an accurate time value. We can rewrite the above program to work on most recent versions of Linux, macOS and BSDs like this:
As a side note, timespec_get, on Posix systems, when available, corresponds to calling clock_gettime with a CLOCK_REALTIME argument.
Another observation is that clock_gettime is not available on Windows.
Let’s implement a portable version of timespec_get:
where time_utils.h contains:
With the above time_utils.c and time_utils.h we can rewrite our initial example in a portable way:
Assuming that the above program was saved as gettime_usage_demo.c, this is how you can build it on Linux, macOS or Windows:
How about delaying program execution for a specified amount of time ? If your C compiler supports C11 threads.h functionality you can use the thrd_sleep function. Unfortunately you won’t be able to use threads.h on Windows or macOS.
Here is an example of modifying our last code to wait for half a second using thrd_sleep, this works only on Linux:
Assuming that you’ve saved the above program as thrd_sleep_demo.c, this is what I see if I build and run the code on a Linux machine:
On Posix systems you can also use the nanosleep function, this has the advantage that it will work on both Linux and macOS. Here is the above code modified to work on Posix systems:
Assuming that you’ve saved the above program as nanosleep_demo.c, this is what I see if I build and run the code on a macOS machine:
As mentioned before, we can’t use the nanosleep function on Windows. A Windows replacement for Posix’s nansleep is the Sleep function. Please note that the Sleep function from Windows accepts as a single argument the time duration in milliseconds of the requested delay. Also, the argument to the Windows Sleep function is of type DWORD which is equivalent to uint32_t.
Using Sleep from Windows and nanosleep from Posix systems, we can write a portable delay function (we’ll put the new function in time_utils.c):
We’ll also need to modify time_utils.h:
Here is an example of using the delay function from above to wait for 1.5 seconds:
If I build and run the code on a Linux machine, this is what I see: