C++ 获取毫秒级时间,跨平台
C++ Get Time in Milliseconds, Cross Platform
我想得到时间:
- 以毫秒为单位(或更短)
- 毫秒分辨率(或更好)
- 使用标准的跨平台方法
- 作为无符号整数
时间戳的实际意义(也就是它自纪元以来的时间还是程序的 运行 时间)一点都不重要,只要它可以用来计算准确的增量时间即可。
我试过 <time.h>
但发现它不是很准确,有人建议不要在另一个答案中使用它。
<chrono>
在其他问题中被推荐,但实际上 return 时间不是整数,而是某种奇怪的对象。
我在<chrono>
中使用的代码是auto time = std::chrono::steady_clock::now()
。
在Java中,这就是System.currentTimeMillis()
。
您需要转换到您期望的持续时间:
std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
我想得到时间:
- 以毫秒为单位(或更短)
- 毫秒分辨率(或更好)
- 使用标准的跨平台方法
- 作为无符号整数
时间戳的实际意义(也就是它自纪元以来的时间还是程序的 运行 时间)一点都不重要,只要它可以用来计算准确的增量时间即可。
我试过 <time.h>
但发现它不是很准确,有人建议不要在另一个答案中使用它。
<chrono>
在其他问题中被推荐,但实际上 return 时间不是整数,而是某种奇怪的对象。
我在<chrono>
中使用的代码是auto time = std::chrono::steady_clock::now()
。
在Java中,这就是System.currentTimeMillis()
。
您需要转换到您期望的持续时间:
std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();