计算时间差值
示例代码
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();//当前时间
std::this_thread::sleep_for(std::chrono::seconds(2));//休眠两秒钟
std::chrono::steady_clock::time_point last = std::chrono::steady_clock::now();//当前时间
std::chrono::nanoseconds dt = last - start;
std::cout << "总共耗时: " << dt.count() << "纳秒" << std::endl;
//转换为秒单位
std::chrono::seconds sds = std::chrono::time_point_cast<std::chrono::seconds>(last) -
std::chrono::time_point_cast<std::chrono::seconds>(start);
std::cout << "总共耗时: " << sds.count() << "秒" << std::endl;
return 0;
}
运行输出