std::clock
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <ctime> 中定义
|
||
std::clock_t clock(); |
||
返回近似使用的处理器时间的过程,因为一个实现定义的时代的开始程序的执行。将结果转换到秒的值除以它的CLOCKS_PER_SEC。因为开始的std::clock时代不具有一致的程序的开始,只由不同的呼叫返回到std::clock两个值之间的差异是有意义的。如果是由其他进程共享CPU,std::clock时间可能会提前比挂钟慢。如果当前进程是多线程的多个执行核心是,std::clock时间可能提前的速度比挂钟.....
原文:
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. Because the beginning of the std::clock era does not have to coincide with the start of the program, only the difference between two values returned by different calls to std::clock is meaningful. If the CPU is shared by other processes, std::clock time may advance slower than wall clock. If the current process is multithreaded and more than one execution core is available, std::clock time may advance faster than wall clock.
目录 |
[编辑] 参数
(无)
[编辑] 返回值
至今(clock_t)(-1)如果该信息不可用程序所使用的处理器时间.
原文:
Processor time used by the program so far or (clock_t)(-1) if that information is unavailable.
[编辑] 注释
在POSIX兼容的系统上,clock_gettime与时钟编号CLOCK_PROCESS_CPUTIME_ID,为您提供更好的分辨率.
原文:
On POSIX-compatible systems, clock_gettime with clock id CLOCK_PROCESS_CPUTIME_ID offers better resolution.
在某些实现中返回的值的
clock()
环绕。例如,在32位std::clock_t的机器,它把,后2147秒或36分钟.原文:
The value returned by
clock()
may wrap around on some implementations. For example, on a machine with 32-bit std::clock_t, it wraps after 2147 seconds or 36 minutes.clock()
并不需要是线程安全的.原文:
clock()
is not required to be thread-safe.[编辑] 示例
这个例子演示了clock()的时间和实际时间之间的差异
原文:
This example demonstrates the difference between clock() time and real time
#include <iostream> #include <chrono> #include <ctime> #include <thread> // the function f() does some time-consuming work void f() { volatile double d; for(int n=0; n<10000; ++n) for(int m=0; m<10000; ++m) d += d*n*m; } int main() { std::clock_t c_start = std::clock(); auto t_start = std::chrono::high_resolution_clock::now(); std::thread t1(f); std::thread t2(f); // f() is called on two threads t1.join(); t2.join(); std::clock_t c_end = std::clock(); auto t_end = std::chrono::high_resolution_clock::now(); std::cout << "CPU time used: " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n"; std::cout << "Wall clock time passed: " << std::chrono::duration_cast<std::chrono::milliseconds>(t_end - t_start).count() << " ms\n"; }
输出:
CPU time used: 1520 ms Wall clock time passed: 769 ms
[编辑] 另请参阅
一个 time_t 对象转换为文本表示原文: converts a time_t object to a textual representation(函数) | |
返回当前时间的划时代的时间,因为系统 原文: returns the current time of the system as time since epoch (函数) | |
C documentation for clock
|