Tweaking @btime

Hello!

The docs of the macro ‘@btime’ says:

The printed time is the minimum elapsed time measured during the benchmark.

Is there a way to make the macro ‘@btime’ print the mean execution time instead of the minimum?

Thanks!

No, it’s hard-coded. However, you can easily copy the macro btime definition and write your own if you want, or just call @benchmark and get all the statistics.

The basic argument for preferring the minimum time is that external noise in the timing measurements is always positive.

1 Like

I think this already exists here

https://github.com/JuliaCI/BenchmarkTools.jl/

EDIT: Ah I guess if you specifically wanted it in @btime then yes see the post below

1 Like

See #258, although “in addition to” not “instead of”.

1 Like

Isn’t the median a better estimator than the mean when the distribution is (possibly very) skewed?

The argument for mean is that this seems the simplest way to get some idea of the cost of allocations. Assuming your function will ultimately be called many times. The reported GC time is much smaller than this.

The PR doesn’t propose that this as a great choice for the only number to look at, only that it’s an informative complement to the minimum.

3 Likes

In code that allocates, I think the mean is the best estimator of throughput.

Alright, thanks a lot for all the hints! :slightly_smiling_face:

Since it hasn’t been explicitly mentioned, note that you get a lot more data (the entire histogram, even) with @benchmark.

3 Likes