Find execution time of a C program
This post will discuss how to find the execution time of a C program in Windows and Linux environments.
There are four commonly used methods to find the execution time of a C program:
1. Using clock()
function
We can use the clock()
function provided by the <time.h>
header file to calculate the CPU time consumed by a task within a C application. It returns the clock_t
type, which stores the total number of clock ticks.
To compute the total number of seconds elapsed, we need to divide the total number of clock ticks elapsed by CLOCKS_PER_SEC
macro (also present in <time.h>
) as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> #include <time.h> // for clock_t, clock(), CLOCKS_PER_SEC #include <unistd.h> // for sleep() // main function to find the execution time of a C program int main() { // to store the execution time of code double time_spent = 0.0; clock_t begin = clock(); // do some stuff here sleep(3); clock_t end = clock(); // calculate elapsed time by finding difference (end - begin) and // dividing the difference by CLOCKS_PER_SEC to convert to seconds time_spent += (double)(end - begin) / CLOCKS_PER_SEC; printf("The elapsed time is %f seconds", time_spent); return 0; } |
Output (may vary):
The elapsed time is 0.000014 seconds
Please note that the clock()
function doesn’t return the actual amount of time elapsed but returns the amount of time taken by the underlying operating system to run the process. In other words, the actual wall clock time might actually be much greater.
2. Using time()
function
The <time.h>
header also provides time()
function that returns the total number of seconds elapsed since the Epoch (00:00:00 UTC, January 1, 1970). It takes the pointer to time_t
as an argument, which is usually passed as NULL
and returns the time_t
type. If the argument is not NULL
, then the return value is also stored in the memory pointed by the argument.
Its usage is similar to the clock()
function, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> #include <time.h> // for time() #include <unistd.h> // for sleep() // main function to find the execution time of a C program int main() { time_t begin = time(NULL); // do some stuff here sleep(3); time_t end = time(NULL); // calculate elapsed time by finding difference (end - begin) printf("The elapsed time is %d seconds", (end - begin)); return 0; } |
Output:
The elapsed time is 3 seconds
3. Using gettimeofday()
function
The gettimeofday()
function returns the wall clock time elapsed since the Epoch and store it in the timeval
structure, expressed as seconds and microseconds.
It is defined in the <sys/time.h>
header file and takes two arguments – the first argument is a reference to the timeval
structure, and the second argument is a null pointer. The timeval
structure is declared as follows by the <time.h>
header:
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
The following code demonstrates the usage of gettimeofday()
by measuring the wall clock time:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> #include <sys/time.h> // for gettimeofday() #include <unistd.h> // for sleep() // main function to find the execution time of a C program int main() { struct timeval start, end; gettimeofday(&start, NULL); // do some stuff here sleep(5); gettimeofday(&end, NULL); long seconds = (end.tv_sec - start.tv_sec); long micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec); printf("The elapsed time is %d seconds and %d micros\n", seconds, micros); return 0; } |
Output (may vary):
The elapsed time is 5 seconds and 5000147 micros
This function is supported by GCC compilers and might not work on Windows.
4. Using clock_gettime()
function
We can also use clock_gettime()
function defined in <time.h>
header file which supports up to nanosecond accuracy. It takes two arguments – the first argument is clock type, and the second argument is a pointer to the timespec
structure. The timespec
structure is provided by the <time.h>
header and is declared as:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
The following code calculates elapsed time using a system-wide real-time clock, identified by CLOCK_REALTIME
, whose time represents seconds and nanoseconds since the Epoch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> #include <time.h> // for clock_t, clock() #include <unistd.h> // for sleep() #define BILLION 1000000000.0 // main function to find the execution time of a C program int main() { struct timespec start, end; clock_gettime(CLOCK_REALTIME, &start); // do some stuff here sleep(3); clock_gettime(CLOCK_REALTIME, &end); // time_spent = end - start double time_spent = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / BILLION; printf("The elapsed time is %f seconds", time_spent); return 0; } |
Please note that the clock_gettime()
function will work only on very few UNIX machines.
That’s all about finding the execution time of a C program.
Related Post:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)