std::chrono::steady_clock代码实例
时间: 2024-03-04 07:46:50 浏览: 82
std::chrono::steady_clock是C++标准库中的一个类,用于测量时间间隔。它提供了一种相对稳定的时钟,适用于计算时间间隔和性能测量。
下面是一个std::chrono::steady_clock的代码示例:
```cpp
#include <iostream>
#include <chrono>
int main() {
// 获取当前时间点
auto start = std::chrono::steady_clock::now();
// 模拟一些耗时操作
for (int i = 0; i < 100000000; ++i) {
// do something
}
// 获取耗时
auto end = std::chrono::steady_clock::now();
auto duration = end - start;
// 将耗时转换为毫秒
double milliseconds = std::chrono::duration<double, std::milli>(duration).count();
std::cout << "耗时: " << milliseconds << " 毫秒" << std::endl;
return 0;
}
```
在这个示例中,我们首先使用`std::chrono::steady_clock::now()`获取当前时间点作为起始时间。然后,我们模拟了一些耗时操作,例如一个循环。接着,我们再次使用`std::chrono::steady_clock::now()`获取当前时间点作为结束时间。通过计算起始时间和结束时间之间的时间间隔,我们可以得到耗时。最后,我们将耗时转换为毫秒并输出。
阅读全文
相关推荐


















