c++ - clock() 对于定时器来说可靠吗?

标签 c++ clock ctime

我正在使用 clock(),我想知道它是否会重置或达到最大值。我使用它的目的只是太受制于之前的函数调用并找出不同之处。

感谢到目前为止的帮助,但我真的无法让 chrono 的东西在 VS '12 中工作,但它很好,因为我认为它比我需要的多一点,我正在考虑使用 的时间( ) 但我不知道如何将 t_time 转换为仅包含当前秒数 0-60 的 int,有什么帮助吗?

最佳答案

就标准而言,

The range and precision of times representable in clock_t and time_t are implementation-defined.

(C99,§7.23.1 ¶4)

因此无法保证范围; clock() 的定义并没有说任何关于回绕的内容,尽管它说了

If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)(-1)

所以我们可能会说超出clock_t的范围可能会被看成是“它的值无法表示”;另一方面,这种解释意味着,一段时间后,clock() 变得完全无用。

事实上,如果我们深入到具体的实现 (glibc),我们会看到:

matteo@teokubuntu:~$ man 3 clock
   Note that  the  time  can  wrap  around.   On  a  32-bit  system  where
   CLOCKS_PER_SEC  equals 1000000 this function will return the same value
   approximately every 72 minutes.

关于c++ - clock() 对于定时器来说可靠吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118231/

相关文章:

c++ - 如何修复程序上的错误以衡量性能

java - 如何在 Java 中使用 BufferedImage 更改模拟时钟的钟面

ubuntu - 是否可以建立一个纯内部NTP系统?

c++ - 命令行输入c++

c++ - 提高编译器中 std::string 的效率

C++:容器替换 vector/deque 的巨大尺寸

c - fprintf 和 ctime 没有从 ctime 传递\n

c - 人类可读的 64 位 time_t 值字符串

ruby - 如何在 Mac OS 上使用 ruby​​ 设置文件创建时间?

c++ - 为类的 AllocatorAwareContainer 数据成员使用自定义分配器