欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

clock

程序员文章站 2024-01-23 18:21:28
...

clock

clock_t clock(void);

1. Clock program
         Returns the processor time consumed by the program.
        The value returned is expressed in clock ticks, which are units of time of a constant but system-specific length (with a relation of CLOCKS_PER_SEC clock ticks per second).
        The epoch used as reference by clock varies between systems, but it is related to the program execution (generally its launch). To calculate the actual processing time of a program, the value returned by clock shall be compared to a value returned by a previous call to the same function.
1.1 Parameters
        none
1.2 Return Value
        The number of clock ticks elapsed since an epoch related to the particular program execution.
        On failure, the function returns a value of -1.
        clock_t is a type defined in <ctime> as an alias of a fundamental arithmetic type.
1.3 Data races
        Concurrently calling this function is safe, causing no data races.
1.4 Exceptions (C++)
       No-throw guarantee: this function never throws exceptions.
2. Example
2.1 example

/*
 ============================================================================
 Name        : clock_example_1.c
 Author      : foreverstrong
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

/* clock example: frequency of primes */
#include <stdio.h>      /* printf */
#include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
#include <math.h>       /* sqrt */

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()
{
	clock_t t;
	int f;
	t = clock();
	printf("Calculating...\n");
	f = frequency_of_primes(99999);
	printf("The number of primes lower than 100,000 is: %d\n", f);
	t = clock() - t;
	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 34633 clicks (0.034633 seconds).
3. clock
        Defined in header <time.h>

clock_t clock(void);
        Returns the approximate processor time used by the process since the beginning of an implementation-defined era related to the program's execution. To convert result value to seconds, divide it by CLOCKS_PER_SEC.
        Only the difference between two values returned by different calls to clock is meaningful, as the beginning of the clock era does not have to coincide with the start of the program. clock time may advance faster or slower than the wall clock, depending on the execution resources given to the program by the operating system. For example, if the CPU is shared by other processes, clock time may advance slower than wall clock. On the other hand, if the current process is multithreaded and more than one execution core is available, clock time may advance faster than wall clock.
3.1 Parameters
        none
3.2 Return value
        Processor time used by the program so far or (clock_t)(-1) if that information is unavailable or its value cannot be represented.
Notes
        On POSIX-compatible systems, clock_gettime with clock id CLOCK_PROCESS_CPUTIME_ID offers better resolution.
        The value returned by clock() may wrap around on some implementations. For example, on a machine with 32-bit clock_t, it wraps after 2147 seconds or 36 minutes.
4. Example
4.1 example

        This example demonstrates the difference between clock() time and real time.

/*
 ============================================================================
 Name        : clock_example_2.c
 Author      : foreverstrong
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

/* clock example: frequency of primes */
#include <stdio.h>      /* printf */
#include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
#include <math.h>       /* sqrt */
#include <threads.h>    /* pthread.h in POSIX threads.h*/

// the function f() does some time-consuming work
int f(void* thr_data) // return void* in POSIX
{
	volatile double d = 0;
	for (int n = 0; n < 10000; ++n)
	{
		for (int m = 0; m < 10000; ++m)
		{
			d += d * n * m;
		}
	}
	return 0;
}

int main(void)
{
	struct timespec ts1, tw1; // both C11 and POSIX
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts1); // POSIX
	clock_gettime(CLOCK_MONOTONIC, &tw1); // POSIX; use timespec_get in C11
	clock_t t1 = clock();

	thrd_t thr1, thr2;  // C11; use pthread_t in POSIX
	thrd_create(&thr1, f, NULL); // C11; use pthread_create in POSIX
	thrd_create(&thr2, f, NULL);
	thrd_join(thr1, NULL); // C11; use pthread_join in POSIX
	thrd_join(thr2, NULL);

	struct timespec ts2, tw2;
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts2);
	clock_gettime(CLOCK_MONOTONIC, &tw2);
	clock_t t2 = clock();

	double dur = 1000.0 * (t2 - t1) / CLOCKS_PER_SEC;
	double posix_dur = 1000.0 * ts2.tv_sec + 1e-6 * ts2.tv_nsec
			- (1000.0 * ts1.tv_sec + 1e-6 * ts1.tv_nsec);
	double posix_wall = 1000.0 * tw2.tv_sec + 1e-6 * tw2.tv_nsec
			- (1000.0 * tw1.tv_sec + 1e-6 * tw1.tv_nsec);

	printf("CPU time used (per clock(): %.2f ms\n", dur);
	printf("CPU time used (per clock_gettime()): %.2f ms\n", posix_dur);
	printf("Wall time passed: %.2f ms\n", posix_wall);
}
        Possible output:
CPU time used (per clock(): 1580.00 ms
CPU time used (per clock_gettime()): 1582.76 ms
Wall time passed: 792.13 ms


        A real-time clock (RTC) is a computer clock (most often in the form of an integrated circuit) that keeps track of the current time.

        Elapsed real time, real time, wall-clock time, or wall time is the actual time taken from the start of a computer program to the end. In other words, it is the difference between the time at which a task finishes and the time at which the task started.

wall clock:挂钟,实际时间

References

http://www.cplusplus.com/reference/ctime/clock/
http://zh.cppreference.com/w/c/chrono/clock
http://en.cppreference.com/w/c/chrono/clock
相关标签: clock