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:

Download  Run Code

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:

Download  Run Code

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:

Download  Run Code

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.

Download Code

 
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:

Measure elapsed time of a C++ program using Chrono library