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

如何在批处理模式下保存BenchmarkTools的直方图以用于后续可视化?

在Julia批处理模式下保存BenchmarkTools直方图的方法

可行,有两种常用实现方案:

方案一:提取基准数据,用绘图库生成并保存可视化直方图

BenchmarkTools的基准结果对象包含所有原始采样数据,可手动提取后借助Plots、StatsPlots这类绘图库生成标准直方图并导出文件,这种方式生成的图表更适合后续可视化需求。

示例代码:

using BenchmarkTools, StatsPlots

# 执行基准测试
result = @benchmark sin(x) setup=(x=rand())

# 提取采样时间数据(单位转换为纳秒)
sample_times = time.(result.times)

# 生成直方图并保存为PNG文件
histogram(sample_times, label="Sin函数基准测试", xlabel="耗时(ns)", ylabel="采样次数")
savefig("benchmark_histogram.png")

方案二:捕获终端文本直方图并保存到文件

如果需要保留BenchmarkTools原生的文本格式直方图,可使用Capture包捕获终端输出,再写入本地文件。

示例代码:

using BenchmarkTools, Capture

# 捕获基准测试及直方图的终端输出
output_content = @capture_out begin
    benchmark_result = @benchmark sin(x) setup=(x=rand())
    display(histogram(benchmark_result))
end

# 将捕获的文本直方图写入文件
open("benchmark_histogram.txt", "w") do file
    write(file, output_content)
end

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 17:47:23