How to find the execution time of a C program

In this post, we will discuss how to find the execution time of a C program in windows and linux environment.


 

There are four commonly used methods to find the execution time of a C program –

 

1. clock()

We can use clock() function provided by <time.h> header file to calculate the CPU time consumed by a task within a C application. It returns clock_t type which stores the number of clock ticks.

In order to compute the number of seconds elapsed, we need to divide the number of clock ticks elapsed by CLOCKS_PER_SEC macro (also present in <time.h>) as shown below:

 

Download   Run Code

Output (may vary):

Time elpased is 0.000014 seconds

 

Please note that 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. time()

The <time.h> header also provides time() function that returns the number of seconds elapsed since the Epoch (00:00:00 UTC, January 1, 1970). It takes pointer to time_t as an argument which is usually passed as NULL and returns time_t type. If the argument is not NULL, then the return value is also stored in the memory pointed by the argument.

It’s usage is similar to clock() function as shown below:

 

Download   Run Code

Output :

Time elpased is 3 seconds

 

3. gettimeofday()

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 <sys/time.h> header file and takes two arguments – the first arugment is reference to the timeval structure and the second argument is a null pointer. The timeval structure is declared as below by the <time.h> header:

struct timeval {
    long tv_sec;  /* seconds */
    long tv_usec; /* microseconds */
};

Below code demonstates the usage of gettimeofday() by measuring the wall clock time:

 

Download   Run Code

Output (may vary):

Time elpased is 5 seconds and 5000147 micros

This function is supported by GCC compilers and might not work on Windows.

 

4. clock_gettime()

We can also use clock_gettime() function defined in <time.h> header file which supports upto nanosecond accuracy. It takes two arguments – the first arugment is clock type and the second argument is a pointer to 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 */
};

Below code calculates elapsed time using system-wide realtime clock, identified by CLOCK_REALTIME whose time represents seconds and nanoseconds since the Epoch.

 

Download

 
Please note that the clock_gettime() function will work only on very few UNIX machines.

 
Related Post: Measure execution time of a C++ program

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz