gpt-4-0314· openai
Suzhen/CodeChat
给出一份运行时间超过1分钟的cpu串行程序
- User
给出一份运行时间超过1分钟的cpu串行程序
- Assistant
以下是一个C++程序,它将运行最大公约数(GCD)计算100000000次,从而使CPU运行时间超过1分钟。
#include <iostream> #include <chrono> #include <cmath> // 计算两个数的最大公约数函数 int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { // 记录执行时间起点 auto start = std::chrono::steady_clock::now(); // 循环次数 const int iterations = 100000000; // 计算次数计数器 long long count = 0; for (int i = 1; i <= iterations; i++) { // 计算相邻的两个数的最大公约数 int a = 100000000 - i; int b = 100000001 - i; // 计算最大公约数 count += gcd(a, b); } // 记录执行时间终点 auto end = std::chrono::steady_clock::now(); // 计算花费的时间 auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start).count(); std::cout << "Total GCDs: " << count << std::endl; std::cout << "Time taken to run: " << duration << " seconds" << std::endl; return 0; }请注意,根据您的CPU性能,运行时间可能会略有不同。 如要根据您的具体情况调整程序运行时间,可随意修改
iterations变量中的值以增加或减少循环次数。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.