You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++ chrono库无法显示完整小数位问题求助

C++中如何输出高精度的执行时间小数位

我编写了一段代码,通过种子42生成1-100范围内的随机底数a和指数n,调用potencia1函数计算幂运算,测量每次执行的OE(操作数)和时间。当前问题是:期望打印精确的时间小数位,但实际输出如0.0006000而非预期的0.0005991,不知如何解决。

代码实现

#include <random>
#include <chrono>
#include <iomanip>

//不输出结果是因为变量的存储位数不足以正确保存结果

using namespace std;

// 迭代式平凡解法
int OE;

long long int potencia1(int a, int n) {
   long long int sol = 1;
   for (int i = 0; i < n; i++) { // 修正循环以完整包含指数
       OE++;
       sol *= a;
       OE += 2;
   }
   OE++;
   return sol;
}

int main() {
   // 用于测量总执行时间的变量
   chrono::duration<double, nano> total_time(0); // 使用纳秒以获得更高精度

   // 种子
   srand(42);

   for (int j = 0; j < 10; j++) {
       int a = rand() % 100 + 1;
       int n = rand() % 100 + 1;
       long long int sol = 1;
       OE = 0; // 每次测试重置OE

       cout << "Prueba " << j + 1 << ":" << endl;
       cout << "La base es: " << a << endl;
       cout << "El exponente es: " << n << endl;

       auto start = chrono::high_resolution_clock::now();
       sol = potencia1(a, n); // 调用修正后的函数
       auto end = chrono::high_resolution_clock::now();

       chrono::duration<double, nano> elapsed = end - start; // 以纳秒为单位测量
       total_time += elapsed;

       // 将纳秒转换为毫秒并以更高精度显示
       double elapsed_ms = elapsed.count() / 1e6;

       // cout << "La solucion es: " << sol << endl;
       cout << "OE: " << OE << endl;
       cout << "Tiempo de ejecucion: " << fixed << setprecision(4) << elapsed_ms << " milisegundos" << endl << endl;
   }

   // 将总时间从纳秒转换为毫秒并以更高精度显示
   double total_time_ms = total_time.count() / 1e6;
   cout << "Tiempo total de ejecucion para 10 pruebas: " << fixed << setprecision(4) << total_time_ms << " milisegundos" << endl;

   return 0;
} 

当前输出示例

La base es: 76
El exponente es: 1
OE: 4
Tiempo de ejecucion: 0.000300 milisegundos

Prueba 2:
La base es: 70
El exponente es: 57
OE: 172
Tiempo de ejecucion: 0.000700 milisegundos

期望输出

希望得到带有更多精确小数位的时间值,例如0.0005991这样的格式。


解决方案

1. 提高setprecision的精度参数

当前代码中setprecision(4)仅保留4位小数,导致输出被截断或补零。要显示更多精确位数,只需增大这个参数值。例如要显示7位小数,修改如下:

修改单次时间输出代码

cout << "Tiempo de ejecucion: " << fixed << setprecision(7) << elapsed_ms << " milisegundos" << endl << endl;

修改总时间输出代码

cout << "Tiempo total de ejecucion para 10 pruebas: " << fixed << setprecision(7) << total_time_ms << " milisegundos" << endl;

调整后,输出会保留7位小数,满足对精度的需求。

2. 转换为更小的时间单位(可选)

如果觉得毫秒级的小数位不够直观,可以转换为微秒(1毫秒=1000微秒)或直接输出纳秒,精度表现更清晰:

输出微秒(保留3位小数)

double elapsed_us = elapsed.count() / 1e3;
cout << "Tiempo de ejecucion: " << fixed << setprecision(3) << elapsed_us << " microsegundos" << endl << endl;

直接输出纳秒

cout << "Tiempo de ejecucion: " << elapsed.count() << " nanosegundos" << endl << endl;

3. 解决极短操作的精度限制

如果目标函数执行时间极短,high_resolution_clock的系统精度可能无法捕捉到足够细节。此时可以通过重复执行函数多次,取平均时间来提升测量精度:

const int repeat = 1000; // 重复执行次数
auto start = chrono::high_resolution_clock::now();
for(int k=0; k<repeat; k++){
    sol = potencia1(a, n);
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double, nano> elapsed = end - start;
double avg_elapsed_ms = (elapsed.count() / repeat) / 1e6; // 计算平均单次时间

内容的提问来源于stack exchange,提问作者user18972374

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 11:57:17