Create cpp_running_time.cpp

This commit is contained in:
2025-09-29 17:30:51 +08:00
parent 1ac4e64cd8
commit f8577aa56d

View File

@@ -0,0 +1,15 @@
#include <iostream>
#include <chrono>
int main() {
double x = 0.0;
auto start = std::chrono::high_resolution_clock::now(); // 记录开始时间
for (long long i = 0; i < 1e10; ++i) {
x += 1e-10; // 模拟一些计算
}
auto end = std::chrono::high_resolution_clock::now(); // 记录结束时间
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start); // 计算时间差
std::cout << "Running time: " << duration.count() << " s" << std::endl;
std::cout << "Result x = " << x << std::endl;
return 0;
}