function
clock
<ctime>
Clock program
Returns the number of clock ticks elapsed since the program was launched.
The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).
The initial moment of reference used by
clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by
clock should be compared to a value returned by an initial call to
clock.
Parameters
(none)
Return Value
The number of clock ticks elapsed since the program start.
On failure, the function returns a value of
-1.
clock_t is a type defined in
<ctime> to some type capable of representing clock tick counts and support arithmetical operations (generally a long integer).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
/* clock example: frequency of primes */
#include <stdio.h>
#include <time.h>
#include <math.h>
int frequency_of_primes (int n) {
int i,j;
int freq=n-1;
for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}
return freq;
}
int main ()
{
int f;
int t;
printf ("Calculating...\n");
f = frequency_of_primes (99999);
t = clock();
printf ("The number of primes lower than 100,000 is: %d\n",f);
printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
return 0;
}
|
Output:
Calculating...
The number of primes lower than 100,000 is: 9592
It took me 143 clicks (0.143000 seconds).
|
See also
- time
- Get current time (function)
- difftime
- Return difference between two times (function)