Skip to content

Commit 4009a7a

Browse files
authored
Merge pull request #255 from JuliaCI/teh/histrange
Support manual range for hist display
2 parents 102a6f0 + 0270da0 commit 4009a7a

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

docs/src/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ julia> @btime sin(x) setup=(x=rand())
3737
0.49587200950472454
3838
```
3939

40+
If you're interested in profiling a fast-running command, you can use `@bprofile sin(x) setup=(x=rand())` and then your favorite
41+
tools for displaying the results (`Profile.print` or a graphical viewer).
42+
4043
If the expression you want to benchmark depends on external variables, you should use [`$` to "interpolate"](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions) them into the benchmark expression to
4144
[avoid the problems of benchmarking with globals](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables).
4245
Essentially, any interpolated variable `$x` or expression `$(...)` is "pre-computed" before benchmarking begins:

docs/src/manual.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,42 @@ julia> loadparams!(suite, BenchmarkTools.load("params.json")[1], :evals, :sample
896896
Caching parameters in this manner leads to a far shorter turnaround time, and more importantly, much more consistent results.
897897

898898
## Visualizing benchmark results
899+
900+
For comparing two or more benchmarks against one another, you can manually specify the range of the histogram using an
901+
`IOContext` to set `:histmin` and `:histmax`:
902+
903+
```julia
904+
julia> io = IOContext(stdout, :histmin=>0.5, :histmax=>8, :logbins=>true)
905+
IOContext(Base.TTY(RawFD(13) open, 0 bytes waiting))
906+
907+
julia> b = @benchmark x^3 setup=(x = rand()); show(io, MIME("text/plain"), b)
908+
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
909+
Range (min max): 1.239 ns 31.433 ns ┊ GC (min max): 0.00% 0.00%
910+
Time (median): 1.244 ns ┊ GC (median): 0.00%
911+
Time (mean ± σ): 1.266 ns ± 0.611 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
912+
913+
914+
▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
915+
0.5 ns Histogram: log(frequency) by time 8 ns <
916+
917+
Memory estimate: 0 bytes, allocs estimate: 0.
918+
julia> b = @benchmark x^3.0 setup=(x = rand()); show(io, MIME("text/plain"), b)
919+
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
920+
Range (min max): 5.636 ns 38.756 ns ┊ GC (min max): 0.00% 0.00%
921+
Time (median): 5.662 ns ┊ GC (median): 0.00%
922+
Time (mean ± σ): 5.767 ns ± 1.384 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
923+
924+
█▆ ▂ ▁
925+
▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁███▄▄▃█▁▁▁▁▁▁▁▁▁▁▁▁ █
926+
0.5 ns Histogram: log(frequency) by time 8 ns <
927+
928+
Memory estimate: 0 bytes, allocs estimate: 0.
929+
930+
```
931+
932+
Set `:logbins` to `true` or `false` to ensure that all use the same vertical scaling (log frequency or frequency).
933+
934+
899935
The `Trial` object can be visualized using the `BenchmarkPlots` package:
900936

901937
```julia

src/trials.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,7 @@ function withtypename(f, io, t)
290290
end
291291
end
292292

293-
function bindata(sorteddata, nbins)
294-
min, max = sorteddata[[1; end]]
293+
function bindata(sorteddata, nbins, min, max)
295294
Δ = (max - min) / nbins
296295
bins = zeros(nbins)
297296
lastpos = 0
@@ -302,6 +301,7 @@ function bindata(sorteddata, nbins)
302301
end
303302
bins
304303
end
304+
bindata(sorteddata, nbins) = bindata(sorteddata, nbins, first(sorteddata), last(sorteddata))
305305

306306
function asciihist(bins, height=1)
307307
histbars = ['', '', '', '', '', '', '', '']
@@ -440,20 +440,25 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
440440
histwidth = 42 + lmaxtimewidth + rmaxtimewidth
441441

442442
histtimes = times[1:round(Int, histquantile*end)]
443-
bins, logbins = bindata(histtimes, histwidth - 1), false
443+
histmin = get(io, :histmin, first(histtimes))
444+
histmax = get(io, :histmax, last(histtimes))
445+
logbins = get(io, :logbins, nothing)
446+
bins = bindata(histtimes, histwidth - 1, histmin, histmax)
444447
append!(bins, [1, floor((1-histquantile) * length(times))])
445448
# if median size of (bins with >10% average data/bin) is less than 5% of max bin size, log the bin sizes
446-
if median(filter(b -> b > 0.1 * length(times) / histwidth, bins)) / maximum(bins) < 0.05
449+
if (logbins === nothing || logbins === true) && median(filter(b -> b > 0.1 * length(times) / histwidth, bins)) / maximum(bins) < 0.05
447450
bins, logbins = log.(1 .+ bins), true
451+
elseif logbins === nothing
452+
logbins = false
448453
end
449454
hist = asciihist(bins, histheight)
450455
hist[:,end-1] .= ' '
451456
maxbin = maximum(bins)
452457

453-
delta1 = (histtimes[end] - histtimes[1]) / (histwidth - 1)
458+
delta1 = (histmax - histmin) / (histwidth - 1)
454459
if delta1 > 0
455-
medpos = 1 + round(Int, (histtimes[length(times) ÷ 2] - histtimes[1]) / delta1)
456-
avgpos = 1 + round(Int, (mean(times) - histtimes[1]) / delta1)
460+
medpos = 1 + round(Int, (histtimes[length(times) ÷ 2] - histmin) / delta1)
461+
avgpos = 1 + round(Int, (mean(times) - histmin) / delta1)
457462
else
458463
medpos, avgpos = 1, 1
459464
end
@@ -470,7 +475,7 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
470475
end
471476

472477
remtrailingzeros(timestr) = replace(timestr, r"\.?0+ " => " ")
473-
minhisttime, maxhisttime = remtrailingzeros.(prettytime.(round.(histtimes[[1; end]], sigdigits=3)))
478+
minhisttime, maxhisttime = remtrailingzeros.(prettytime.(round.([histmin, histmax], sigdigits=3)))
474479

475480
print(io, "\n", pad, " ", minhisttime)
476481
caption = "Histogram: " * ( logbins ? "log(frequency)" : "frequency" ) * " by time"

test/ExecutionTests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ tune!(b)
135135
# test kwargs separated by `,`
136136
@benchmark(output=sin(x), setup=(x=1.0; output=0.0), teardown=(@test output == sin(x)))
137137

138+
io = IOBuffer()
139+
ioctx = IOContext(io, :histmin=>0.5, :histmax=>8, :logbins=>false)
140+
b = @benchmark x^3 setup=(x = rand()); show(ioctx, MIME("text/plain"), b)
141+
b = @benchmark x^3.0 setup=(x = rand()); show(ioctx, MIME("text/plain"), b)
142+
str = String(take!(io))
143+
idx = findfirst(r"0.5 ns +Histogram: frequency by time +8 ns", str)
144+
@test isa(idx, UnitRange)
145+
idx = findnext( r"0.5 ns +Histogram: frequency by time +8 ns", str, idx[end]+1)
146+
@test isa(idx, UnitRange)
147+
138148
#############
139149
# @bprofile #
140150
#############

0 commit comments

Comments
 (0)