C++ 耗时统计代码片段

C++ 耗时统计代码片段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <chrono>

typedef std::chrono::milliseconds ms;
using clk = std::chrono::system_clock;

void do_my_work() {
// work code here

}

int main() {
auto begin_time = clk::now();
do_my_work();
auto end_time = clk::now();

auto duration_nn = std::chrono::duration_cast<ms>(end_time - begin_time);
std::cout << "timecost: " << (double)duration_nn.count() << " ms" << std::endl;
return 0;
}