C++ Timer Code Example
Here is a basic example of a C++ timer code:
#include <iostream>
#include <chrono>
int main() {
// 获取当前时间
auto start = std::chrono::high_resolution_clock::now();
// 执行需要计时的代码
for (int i = 0; i < 1000000; ++i) {
// do something
}
// 获取结束时间
auto end = std::chrono::high_resolution_clock::now();
// 计算时间差
std::chrono::duration<double> duration = end - start;
// 输出计时结果
std::cout << "Execution time: " << duration.count() << " seconds" << std::endl;
return 0;
}
In this example, we are using std::chrono::high_resolution_clock to get the current time and calculate the time difference to get the execution time of the code. You can replace the code that needs to be timed with your own code.