# Save @btime output

**URL:** <https://discourse.julialang.org/t/save-btime-output/53595>\
**Category:** New to Julia\
**Tags:** question, benchmarktools\
**Created:** [January 19, 2021, 8:43am UTC](https://discourse.julialang.org/t/save-btime-output/53595 "2021-01-19T08:43:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [January 19, 2021, 8:43am UTC](https://discourse.julialang.org/t/save-btime-output/53595/1 "2021-01-19T08:43:40Z")

</div>

Is there a simple way to do this?

I just want to save the time `n` in milliseconds e.g. `n = @btime foo` for some benchmark like:

```julia
check = nothing

function checkaaa()
    rand(10) * rand(10)'
    return nothing
end

check = @btime checkaaa()
@test check !== nothing

julia> @test check !== nothing
Test Failed at REPL[397]:1
  Expression: check !== nothing
   Evaluated: nothing !== nothing
ERROR: There was an error during testing

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 19, 2021, 8:54am UTC](https://discourse.julialang.org/t/save-btime-output/53595/2 "2021-01-19T08:54:07Z")

</div>

`@btime` just _prints_ the result (just like `@time`). If you want something back you should use `@benchmark`:

```julia
julia> using BenchmarkTools

julia> b = @benchmark rand(2,2);

julia> b
BenchmarkTools.Trial: 
  memory estimate: 112 bytes
  allocs estimate: 1
  --------------
  minimum time: 40.862 ns (0.00% GC)
  median time: 43.809 ns (0.00% GC)
  mean time: 46.494 ns (1.74% GC)
  maximum time: 430.152 ns (88.88% GC)
  --------------
  samples: 10000
  evals/sample: 988

julia> min = minimum(b)
BenchmarkTools.TrialEstimate: 
  time: 40.862 ns
  gctime: 0.000 ns (0.00%)
  memory: 112 bytes
  allocs: 1

julia> min.time
40.86234817813765

```

---

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [January 19, 2021, 8:54am UTC](https://discourse.julialang.org/t/save-btime-output/53595/3 "2021-01-19T08:54:52Z")

</div>

excellent, thanks

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 19, 2021, 11:57am UTC](https://discourse.julialang.org/t/save-btime-output/53595/4 "2021-01-19T11:57:10Z")

</div>

Note that you can directly use `@btime` to directly get the timing:

```julia
julia> using BenchmarkTools

julia> minimum(@benchmark(rand(2,2))).time / 1e9
4.744287158746209e-8

julia> @belapsed rand(2,2)
4.755668016194332e-8

```

---

<div class="post-metadata">

**Author:** ![giancarloantonucci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giancarloantonucci/32/202551_2.png) [@giancarloantonucci](https://discourse.julialang.org/u/giancarloantonucci)\
**Post date:** [April 12, 2021, 2:12pm UTC](https://discourse.julialang.org/t/save-btime-output/53595/5 "2021-04-12T14:12:32Z")

</div>

What if I want both the operation result and the timing? In other words, is there an equivalent of `@timed` in BenchmarkTools?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 12, 2021, 3:52pm UTC](https://discourse.julialang.org/t/save-btime-output/53595/6 "2021-04-12T15:52:58Z")

</div>

This is asked every other week, so probably we should have it directly in `BenchmarkTools`:

```julia
julia> @eval BenchmarkTools macro btimed(args...)
           _, params = prunekwargs(args...)
           bench, trial, result = gensym(), gensym(), gensym()
           trialmin, trialallocs = gensym(), gensym()
           tune_phase = hasevals(params) ? :() : :($BenchmarkTools.tune!($bench))
           return esc(quote
               local $bench = $BenchmarkTools.@benchmarkable $(args...)
               $BenchmarkTools.warmup($bench)
               $tune_phase
               local $trial, $result = $BenchmarkTools.run_result($bench)
               local $trialmin = $BenchmarkTools.minimum($trial)
               $result, $BenchmarkTools.time($trialmin)
           end)
       end
@btimed (macro with 1 method)

julia> BenchmarkTools.@btimed sin(12.3)
(-0.26323179136580094, 1.41)

```

(the time is returned in nanoseconds)

---

<div class="post-metadata">

**Author:** ![giancarloantonucci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giancarloantonucci/32/202551_2.png) [@giancarloantonucci](https://discourse.julialang.org/u/giancarloantonucci)\
**Post date:** [April 12, 2021, 4:51pm UTC](https://discourse.julialang.org/t/save-btime-output/53595/7 "2021-04-12T16:51:36Z")

</div>

Thanks! Personally, given how much I rely on `@timed`, having it in BenchmarkTools would be extremely convenient.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 1, 2023, 9:52am UTC](https://discourse.julialang.org/t/save-btime-output/53595/8 "2023-04-01T09:52:57Z")

</div>

Or also using a custom macro with what one needs…

```julia
julia> using BenchmarkTools
julia> macro binfo(bexpr)
           runexpr = quote
             b = @benchmark $bexpr
             return (median(b).time, median(b).memory, median(b).allocs)
           end
           return eval(runexpr)
       end
@binfo (macro with 1 method)
julia> res = @binfo rand(2,2)
(91.87983706720978, 96, 1)

```
