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

如何修复Python程序执行时间测量的Excel列显示异常问题?

问题原因及修复方案

核心问题

你创建的time_row字典中,第一个键为'Total Execution time (s):'(末尾多了一个冒号),但初始化DataFrame时定义的列名是'Total Execution time (s)',两者不匹配,导致数据无法正确对应到目标列,最终Excel输出出现异常。

修复代码

将time_row的键修正为与DataFrame列名完全一致的内容:

time_row={'Total Execution time (s)': wall_clock_time, 'CPU Execution time (s)': res}

可选优化

  1. 移除重复的执行时间计算代码(你重复写了两遍wall_clock_time = et_wall - st_wall和res = et_process - st),保留一次即可。
  2. 新版本Pandas已弃用append方法,推荐使用pd.concat实现数据追加,修改后的代码片段如下:
time_row_df = pd.DataFrame([{'Total Execution time (s)': wall_clock_time, 'CPU Execution time (s)': res}])
df_time = pd.concat([df_time, time_row_df], ignore_index=True)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 09:00:40