How to get BenchmarkTools times in the same unit?

I use BenchmarkTools like this to save and compare performance:

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?

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

julia> using BenchmarkTools

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

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.

3 Likes

Consider also its replacement:

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!