C++计算程序运行时间

引言

有时候我们需要简单计算下程序的运行时间,但又不想借助工具,而是简单的几行代码来粗略计算下时间,话不多说我们直接开始吧。

chrono库使用

C++11中可以通过该库来实现,示例代码如下:

#include <iostream>
#include <chrono>

void func() {
	int arr[1000][100];
	for (int j = 0; j < 100; ++j) {
		for (int i = 0; i < 1000; ++i) {
			arr[i][j] = 1;
		}
	}
}

int main() {
  auto beforeTime = std::chrono::steady_clock::now();
  func();
  auto afterTime = std::chrono::steady_clock::now();
  double duration_millsecond = std::chrono::duration<double, std::milli>(afterTime - beforeTime).count();
  std::cout << "total cost " << duration_millsecond << "ms" << std::endl;
}

执行结果如下:

total cost 0.460439ms

如果我们多处地方都要计算执行时间,那就要重复去写好几遍这段代码,不太方便。所以可以简单封装下:

#include <iostream>
#include <chrono>

template<class T>
inline void costTime(void(*f)(), const std::string& info, const std::string& unit)
{
  auto beforeTime = std::chrono::steady_clock::now();
  f(); 
  auto afterTime = std::chrono::steady_clock::now();
  double duration_millsecond = T(afterTime - beforeTime).count();
  std::cout << info << " total cost " << duration_millsecond << unit << std::endl;
}

// 计算秒数
inline void costTimeBySec(void(*f)(), const std::string& info)
{
	costTime<std::chrono::duration<double>>(f, info, "s");
}
// 计算毫秒数
inline void costTimeByMs(void(*f)(), const std::string& info)
{
	costTime<std::chrono::duration<double, std::milli>>(f, info, "ms");
}
// 计算微秒数
inline void costTimeByUs(void(*f)(), const std::string& info)
{
	costTime<std::chrono::duration<double, std::micro>>(f, info, "us");
}
// 计算纳秒数
inline void costTimeByNs(void(*f)(), const std::string& info)
{
	costTime<std::chrono::duration<double, std::nano>>(f, info, "ns");
}
// 以上接口也可以封装到头文件,下次使用直接包含即可。

// 以下函数实现就都不写了
void func(); 
void func2(int n);
int func3(int n, int m); 

int main() {
	costTimeBySec(func, "func");
	costTimeByMs([](){ func2(1); }, "func2(1)"); // 传入lambda表达式
	costTimeByUs([](){ func3(1, 2); }, "func3(1, 2)"); // 传入lambda表达式
}

控制时间粒度的模板参数如下:

模板参数 级别
std::milli 毫秒
std::micro 微秒
std::nano 纳秒

duration的第二个模板参数不写时,即为秒级别时间。

相关推荐

  1. C++计算程序运行时间

    2024-04-12 03:36:03       36 阅读
  2. C语言)精确计算程序运行时间的方法

    2024-04-12 03:36:03       58 阅读
  3. 用python计算程序运行时间

    2024-04-12 03:36:03       57 阅读
  4. pta 计算火车运行时间 (15分)c语言

    2024-04-12 03:36:03       37 阅读
  5. pta 计算火车运行时间

    2024-04-12 03:36:03       45 阅读
  6. C++】计算代码中程序时间差

    2024-04-12 03:36:03       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 03:36:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 03:36:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 03:36:03       82 阅读
  4. Python语言-面向对象

    2024-04-12 03:36:03       91 阅读

热门阅读

  1. 为无网环境安装golang

    2024-04-12 03:36:03       28 阅读
  2. mybatis的include和sql的使用

    2024-04-12 03:36:03       36 阅读
  3. 回调函数详细介绍(C & C++代码实例)

    2024-04-12 03:36:03       40 阅读
  4. Codeforces Round 515 (Div. 3)

    2024-04-12 03:36:03       33 阅读
  5. Python 推导式介绍

    2024-04-12 03:36:03       32 阅读
  6. 爬虫 xpath基础

    2024-04-12 03:36:03       37 阅读
  7. 栈中的中缀表达式转变为后缀表达式

    2024-04-12 03:36:03       34 阅读
  8. 【综合分析类】校园霸凌

    2024-04-12 03:36:03       36 阅读
  9. 练习4-10 找出最小值

    2024-04-12 03:36:03       29 阅读
  10. sqlplus / as sysdba下中文乱码问题

    2024-04-12 03:36:03       35 阅读
  11. DBA常用命令:数据导出和数据导入

    2024-04-12 03:36:03       29 阅读