5.3 clock()

View Categories

5.3 clock()

3 min read

Overview #

The clock() function returns the processor time (CPU time) consumed by the current program.

Unlike time() and gettimeofday(), which return the current calendar time, clock() measures the amount of CPU time used by the program since it started executing.

It is commonly used to measure the execution time of algorithms and compare program performance.

Header File #

#include <time.h>

Man Pages #

man 3 clock

Function Prototype #

clock_t clock(void);

Parameters #

This function does not accept any parameters.

Return Value #

On success, clock() returns the processor time consumed by the program as a value of type clock_t.

On failure, it returns:

(clock_t)-1

Data Types #

clock_t #

clock_t is a standard C data type used to represent processor time.

The value returned by clock() is expressed in clock ticks, not seconds.

To convert clock ticks to seconds, divide the returned value by CLOCKS_PER_SEC.

CLOCKS_PER_SEC #

CLOCKS_PER_SEC is a macro that specifies the number of clock ticks per second.

It is defined in <time.h>.

Examples #

Example 1 โ€“ Measure CPU Execution Time #

main.c #

#include <stdio.h>
#include <time.h>

int main(void)
{
    clock_t start, end;
    double cpu_time;

    start = clock();

    for (volatile long i = 0; i < 100000000; i++);

    end = clock();

    cpu_time = (double)(end - start) / CLOCKS_PER_SEC;

    printf("CPU Time = %.6f seconds\n", cpu_time);

    return 0;
}

Output #

CPU Time = 0.218000 seconds

The output will vary depending on your processor and compiler optimization.

Explanation #

start = clock();

Stores the processor time before executing the loop.

for (volatile long i = 0; i < 100000000; i++);

Consumes processor time by repeatedly executing the loop.

The volatile keyword prevents the compiler from optimizing away the loop.

end = clock();

Stores the processor time after the loop completes.

cpu_time = (double)(end - start) / CLOCKS_PER_SEC;

Calculates the processor time consumed by the loop in seconds.

printf("CPU Time = %.6f seconds\n", cpu_time);

Displays the CPU execution time.


Example 2 โ€“ Measure Function Execution Time #

main.c #

#include <stdio.h>
#include <time.h>

void calculate(void)
{
    for (volatile long i = 0; i < 50000000; i++);
}

int main(void)
{
    clock_t start, end;

    start = clock();

    calculate();

    end = clock();

    printf("Execution Time = %.6f seconds\n",
           (double)(end - start) / CLOCKS_PER_SEC);

    return 0;
}

Output #

Execution Time = 0.108000 seconds

The output will vary depending on your system.

Explanation #

start = clock();

Records the processor time before calling calculate().

calculate();

Executes the function whose CPU execution time is to be measured.

end = clock();

Records the processor time after the function returns.

(double)(end - start) / CLOCKS_PER_SEC

Computes the total processor time consumed by the function.

Notes #

  • clock() measures CPU time, not calendar time.
  • The returned value is expressed in clock ticks.
  • Divide the returned value by CLOCKS_PER_SEC to obtain the execution time in seconds.
  • Time spent while the program is sleeping or waiting for user input is generally not included in the CPU time.
  • clock() is a C library function, not a Linux system call.

Related Functions #

  • time()
  • gettimeofday()
  • clock_gettime()
  • difftime()

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *