本文主要是介绍C++11 - 使用std::chrono计算程序、函数运行时间,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 使用std::chrono计算程序运行时间
参考代码如下:
#include <iostream>
#include <string>
#include <chrono>void Run()
{for (int i = 0; i < 1000000000; ++i){}}int main()
{auto beforeTime = std::chrono::steady_clock::now();Run();auto afterTime = std::chrono::steady_clock::now();std::cout << "总耗时:" << std::endl;//秒double duration_second = std::chrono::duration<double>(afterTime - beforeTime).count();std::cout << duration_second << "秒" << std::endl;//毫秒级double duration_millsecond = std::chrono::duration<double, std::milli>(afterTime - beforeTime).count();std::cout << duration_millsecond << "毫秒" << std::endl;//微妙级double duration_microsecond = std::chrono::duration<double, std::micro>(afterTime - beforeTime).count();std::cout << duration_microsecond << "微秒" << std::endl;//纳秒级double duration_nanosecond = std::chrono::duration<double, std::nano>(afterTime - beforeTime).count();std::cout << duration_nanosecond << "纳秒" << std::endl;getchar();return 0;
}
如果有兴趣可以访问我的个站:https://www.stubbornhuang.com,更多干货等着您!
这篇关于C++11 - 使用std::chrono计算程序、函数运行时间的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!