Skip to content

Commit c209ed2

Browse files
authored
change intro example benchmark to thwart constant propagation (#136)
1 parent 63be4cd commit c209ed2

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,34 @@ If you're benchmarking on Linux, I wrote up a series of [tips and tricks](https:
3131

3232
## Quick Start
3333

34-
The simplest usage is via the [`@btime` macro](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#benchmarking-basics), which is analogous to Julia's built-in [`@time` macro](https://docs.julialang.org/en/stable/stdlib/base/#Base.@time) but is often more accurate (by collecting results over multiple runs):
34+
The primary macro provided by BenchmarkTools is `@benchmark`:
3535

3636
```julia
3737
julia> using BenchmarkTools
3838

39-
julia> @btime sin(1)
40-
15.081 ns (0 allocations: 0 bytes)
41-
0.8414709848078965
39+
# The `setup` expression is run once per sample, and is not included in the
40+
# timing results. Note that each sample can require multiple evaluations
41+
# benchmark kernel evaluations. See the BenchmarkTools manual for details.
42+
julia> @benchmark sin(x) setup=(x=rand())
43+
BenchmarkTools.Trial:
44+
memory estimate: 0 bytes
45+
allocs estimate: 0
46+
--------------
47+
minimum time: 4.248 ns (0.00% GC)
48+
median time: 4.631 ns (0.00% GC)
49+
mean time: 5.502 ns (0.00% GC)
50+
maximum time: 60.995 ns (0.00% GC)
51+
--------------
52+
samples: 10000
53+
evals/sample: 1000
54+
```
55+
56+
For quick sanity checks, one can use the [`@btime` macro](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#benchmarking-basics), which is a convenience wrapper around `@benchmark` whose output is analogous to Julia's built-in [`@time` macro](https://docs.julialang.org/en/stable/stdlib/base/#Base.@time):
57+
58+
```julia
59+
julia> @btime sin(x) setup=(x=rand())
60+
4.361 ns (0 allocations: 0 bytes)
61+
0.49587200950472454
4262
```
4363

4464
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
@@ -50,10 +70,10 @@ julia> A = rand(3,3);
5070

5171
julia> @btime inv($A); # we interpolate the global variable A with $A
5272
1.191 μs (10 allocations: 2.31 KiB)
53-
73+
5474
julia> @btime inv($(rand(3,3))); # interpolation: the rand(3,3) call occurs before benchmarking
5575
1.192 μs (10 allocations: 2.31 KiB)
56-
76+
5777
julia> @btime inv(rand(3,3)); # the rand(3,3) call is included in the benchmark time
5878
1.295 μs (11 allocations: 2.47 KiB)
5979
```

0 commit comments

Comments
 (0)