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