# Output of @benchmark to string or table?

**URL:** <https://discourse.julialang.org/t/output-of-benchmark-to-string-or-table/27977>\
**Category:** New to Julia\
**Created:** [August 26, 2019, 12:38am UTC](https://discourse.julialang.org/t/output-of-benchmark-to-string-or-table/27977 "2019-08-26T00:38:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmatzke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nmatzke/32/7853_2.png) [@nmatzke](https://discourse.julialang.org/u/nmatzke)\
**Post date:** [August 26, 2019, 12:38am UTC](https://discourse.julialang.org/t/output-of-benchmark-to-string-or-table/27977/1 "2019-08-26T00:38:54Z")

</div>

Hi all!

This is a very simple question but I can’t find an easy answer. Any help appreciated!

I am trying to @benchmark the speed of a function over several orders-of-magnitude of inputs. I would like to just do a for-loop and then capture the benchmark output in a string (or, better, would be an object that I could query).

When I store the output of @benchmark in a variable x, though, I get some raw data that I’d rather not have to process. (Yes, in this case I can figure it out, but in general, sometimes I’d like to capture the screen summary output of a function in a string.)

Simple example code below. Any help welcome!!  
Cheers, Nick

```julia
using BenchmarkTools # for @benchmark
using Statistics # for mean

function example_add(a,b)
	c = a+b
end

x = @benchmark example_add(1, 2)

x

# Screen output -- how do I capture to a string or other variable?
#
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 0.025 ns (0.00% GC)
# median time: 0.029 ns (0.00% GC)
# mean time: 0.029 ns (0.00% GC)
# maximum time: 6.074 ns (0.00% GC)
# --------------
# samples: 10000
# evals/sample: 1000

minimum(x.times)
mean(x.times)
maximum(x.times)

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [August 26, 2019, 3:50am UTC](https://discourse.julialang.org/t/output-of-benchmark-to-string-or-table/27977/2 "2019-08-26T03:50:16Z")

</div>

```julia
julia> using BenchmarkTools

julia> b = @benchmark 2+3
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 0.035 ns (0.00% GC)
  median time: 0.036 ns (0.00% GC)
  mean time: 0.038 ns (0.00% GC)
  maximum time: 12.984 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> show(io, "text/plain", b)

julia> s = String(take!(io))
"BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 0.035 ns (0.00% GC)\n median time: 0.036 ns (0.00% GC)\n mean time: 0.038 ns (0.00% GC)\n maximum time: 12.984 ns (0.00% GC)\n --------------\n samples: 10000\n evals/sample: 1000"

julia> typeof(s)
String

julia> println(s)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 0.035 ns (0.00% GC)
  median time: 0.036 ns (0.00% GC)
  mean time: 0.038 ns (0.00% GC)
  maximum time: 12.984 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

```

---

<div class="post-metadata">

**Author:** ![nmatzke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nmatzke/32/7853_2.png) [@nmatzke](https://discourse.julialang.org/u/nmatzke)\
**Post date:** [August 28, 2019, 1:52am UTC](https://discourse.julialang.org/t/output-of-benchmark-to-string-or-table/27977/3 "2019-08-28T01:52:33Z")

</div>

Thanks very much! That’s a great solution!  
Cheers, Nick

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 28, 2019, 2:20am UTC](https://discourse.julialang.org/t/output-of-benchmark-to-string-or-table/27977/4 "2019-08-28T02:20:15Z")

</div>

> [@nmatzke](#):
>
> (or, better, would be an object that I could query).

The return value of `@benchmark` _is_ an object that you can query:

```julia
julia> using BenchmarkTools, Statistics

julia> x = @benchmark sin(3)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 0.036 ns (0.00% GC)
  median time: 0.039 ns (0.00% GC)
  mean time: 0.039 ns (0.00% GC)
  maximum time: 0.083 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

julia> minimum(x.times)
0.036

julia> maximum(x.times)
0.083

julia> mean(x.times)
0.03916590000000001

julia> median(x.times)
0.039

julia> length(x.times)
10000

```

You can do `dump(x)` to see the fields of this data structure listed explicitly.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 31, 2025, 11:14am UTC](https://discourse.julialang.org/t/output-of-benchmark-to-string-or-table/27977/5 "2025-07-31T11:14:10Z")

</div>

A post was split to a new topic: [How to extract histogram from @benchmark results](https://discourse.julialang.org/t/how-to-extract-histogram-from-benchmark-results/131245)
