以下是一个使用std::chrono库的c++计时代码,计时单位为毫秒:

#include <iostream>
#include <chrono>

int main() {
    auto start = std::chrono::high_resolution_clock::now(); // 开始计时
    // 在这里放置需要计时的代码
    auto end = std::chrono::high_resolution_clock::now(); // 结束计时
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); // 计算耗时,单位为毫秒
    std::cout << "耗时 " << duration.count() << " 毫秒" << std::endl;
    return 0;
}

在上面的代码中,使用了std::chrono::high_resolution_clock来获取当前时间,使用std::chrono::duration_cast来计算时间差,并将时间差转换为毫秒单位。最后输出计时结果。

写一个c++-计时的代码使用相对时间单位以毫秒为单位

原文地址: https://www.cveoy.top/t/topic/gOu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录