# How to get BenchmarkTools times in the same unit?

**URL:** <https://discourse.julialang.org/t/how-to-get-benchmarktools-times-in-the-same-unit/121395>\
**Category:** General Usage\
**Tags:** question, performance, benchmarktools\
**Created:** [October 16, 2024, 6:18pm UTC](https://discourse.julialang.org/t/how-to-get-benchmarktools-times-in-the-same-unit/121395 "2024-10-16T18:18:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [October 16, 2024, 6:18pm UTC](https://discourse.julialang.org/t/how-to-get-benchmarktools-times-in-the-same-unit/121395/1 "2024-10-16T18:18:27Z")

</div>

I use `BenchmarkTools` like this to save and compare performance:

```julia
using BenchmarkTools
b = @benchmark sum(0:2:1_000_000)
t = sum(b.times)/length(b.times)
println(t)

```

The issue is that for some functions it returns times in seconds (`s`) for others in milliseconds (`ms`), microseconds (`μs`) or nanoseconds (`ns`). I have not found a way to figure out in what unit (`s`, `ms`, `μs`, `ns`) the times are returned. I need this to compare performance performance over different functions.

Any idea how to solve this or the set options to always get times in `ns`?

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [October 16, 2024, 6:33pm UTC](https://discourse.julialang.org/t/how-to-get-benchmarktools-times-in-the-same-unit/121395/2 "2024-10-16T18:33:33Z")

</div>

> [@Brinkhuis](#):
>
> The issue is that for some functions it returns times in seconds (`s`) for others in milliseconds (`ms`), microseconds (`μs`) or nanoseconds (`ns`).

Could you provide some examples? For everything I tested I got times in nanoseconds. E.g.

```julia-repl
julia> using BenchmarkTools

julia> b = @benchmark sleep(1); b.times
5-element Vector{Float64}:
 1.0142288e9
 1.0078232e9
 1.0164577e9
 1.0055486e9
 1.0031065e9

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [October 16, 2024, 6:48pm UTC](https://discourse.julialang.org/t/how-to-get-benchmarktools-times-in-the-same-unit/121395/3 "2024-10-16T18:48:32Z")

</div>

The printing is done in varying units. But if you want to access the actual times programmatically by manipulating the output of `@benchmark` or `@belapsed`, indeed they are always in nanoseconds.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 16, 2024, 7:30pm UTC](https://discourse.julialang.org/t/how-to-get-benchmarktools-times-in-the-same-unit/121395/4 "2024-10-16T19:30:56Z")

</div>

Consider also its replacement:

> **[How to migrate from BenchmarkTools to Chairmarks | Chairmarks.jl](https://chairmarks.lilithhafner.com/v1.2.2/migration)**
>
> Documentation for Chairmarks.jl

> It preserves the keyword arguments samples, evals, and seconds. Unlike BenchmarkTools, the seconds argument is honored even as it drops down to the order of 30μs

It seems great (actually both packages), also from Lilith: BasicAutoloads.jl (helping for both packages and more). Everything Lilith does/touches seems great!
