The following application computes the time needed for a process to finish using the method `clock()`

.

The result of the application is the time in seconds as a floating number (where 1.0 = 1 second).

It provides greater accuracy than seconds as the estimation is done using processor time used by the program.

#include <stdio.h> #include <time.h> #include <stdlib.h> #include <limits.h> int main() { /* clock_t clock(void) The clock() function returns an approximation of processor time used by the program. The value returned is the CPU time used so far as a clock_t, to get the number of seconds used, divide by CLOCKS_PER_SEC. On error it returns -1. */ const clock_t start = clock(); /* svoid srand(unsigned int __seed) The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. If no seed value is provided, the rand() function is automatically seeded with a value of 1. */ /* time_t time(time_t *__timer) time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds. If the __timer variable is not NULL, the return value is also stored there. */ srand(time(NULL)); unsigned long i; for (i = 0; i < 10000000; i++) { /* int rand(void) The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive. */ rand(); } const clock_t end = clock(); /* ISO/IEC 9899:1999 7.23.1: Components of time The macro `CLOCKS_PER_SEC' is an expression with type `clock_t' that is the number per second of the value returned by the `clock' function. */ /* CAE XSH, Issue 4, Version 2: <time.h> The value of CLOCKS_PER_SEC is required to be 1 million on all XSI-conformant systems. */ const float seconds = (float) (end - start) / CLOCKS_PER_SEC; printf("Seconds elapsed %f\n", seconds); return 0; }

This post is also available in: Greek