Skip to content

Commit 6af30cf

Browse files
committed
docs
1 parent 1666701 commit 6af30cf

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

docs/src/index.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ For quick sanity checks, one can use the [`@btime` macro](https://github.com/Jul
3333

3434
```julia
3535
julia> @btime sin(x) setup=(x=rand())
36-
4.361 ns (0 allocations: 0 bytes)
36+
min 3.041 ns, mean 3.487 ns (0 allocations)
3737
0.49587200950472454
3838
```
3939

@@ -45,16 +45,16 @@ If the expression you want to benchmark depends on external variables, you shoul
4545
Essentially, any interpolated variable `$x` or expression `$(...)` is "pre-computed" before benchmarking begins:
4646

4747
```julia
48-
julia> A = rand(3,3);
48+
julia> A = rand(8,8);
4949

5050
julia> @btime inv($A); # we interpolate the global variable A with $A
51-
1.191 μs (10 allocations: 2.31 KiB)
51+
min 951.000 ns, mean 1.349 μs (4 allocations, 4.81 KiB)
5252

53-
julia> @btime inv($(rand(3,3))); # interpolation: the rand(3,3) call occurs before benchmarking
54-
1.192 μs (10 allocations: 2.31 KiB)
53+
julia> @btime inv($(rand(8,8))); # interpolation: the rand() call occurs before benchmarking
54+
min 930.542 ns, mean 1.363 μs (4 allocations, 4.81 KiB)
5555

56-
julia> @btime inv(rand(3,3)); # the rand(3,3) call is included in the benchmark time
57-
1.295 μs (11 allocations: 2.47 KiB)
56+
julia> @btime inv(rand(8,8)); # the rand() call is included in the benchmark time
57+
min 1.092 μs, mean 1.572 μs (5 allocations, 5.38 KiB)
5858
```
5959

6060
Sometimes, interpolating variables into very simple expressions can give the compiler more information than you intended, causing it to "cheat" the benchmark by hoisting the calculation out of the benchmark code
@@ -63,13 +63,13 @@ julia> a = 1; b = 2
6363
2
6464

6565
julia> @btime $a + $b
66-
0.024 ns (0 allocations: 0 bytes)
66+
min 0.875 ns, mean 0.950 ns (0 allocations)
6767
3
6868
```
6969
As a rule of thumb, if a benchmark reports that it took less than a nanosecond to perform, this hoisting probably occured. You can avoid this by referencing and dereferencing the interpolated variables
7070
```julia
7171
julia> @btime $(Ref(a))[] + $(Ref(b))[]
72-
1.277 ns (0 allocations: 0 bytes)
72+
min 0.875 ns, mean 0.953 ns (0 allocations)
7373
3
7474
```
7575

docs/src/manual.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ before returning the value of the expression, while `@belapsed`
6565
returns the minimum time in seconds.
6666

6767
```julia
68-
julia> @btime sin(1)
69-
13.612 ns (0 allocations: 0 bytes)
70-
0.8414709848078965
68+
julia> @btime sin.(0:2)
69+
min 25.687 ns, mean 28.266 ns (1 allocation, 80 bytes)
70+
3-element Vector{Float64}:
71+
0.0
72+
0.8414709848078965
73+
0.9092974268256817
7174

72-
julia> @belapsed sin(1)
73-
1.3614228456913828e-8
75+
julia> @belapsed sin.(0:2)
76+
2.5727911646586344e-8
7477
```
7578

7679
### Benchmark `Parameters`
@@ -280,13 +283,13 @@ julia> a = 1; b = 2
280283
2
281284

282285
julia> @btime $a + $b
283-
0.024 ns (0 allocations: 0 bytes)
286+
min 0.833 ns, mean 0.944 ns (0 allocations)
284287
3
285288
```
286289
in this case julia was able to use the properties of `+(::Int, ::Int)` to know that it could safely replace `$a + $b` with `3` at compile time. We can stop the optimizer from doing this by referencing and dereferencing the interpolated variables
287290
```julia
288291
julia> @btime $(Ref(a))[] + $(Ref(b))[]
289-
1.277 ns (0 allocations: 0 bytes)
292+
min 0.875 ns, mean 0.948 ns (0 allocations)
290293
3
291294
```
292295

0 commit comments

Comments
 (0)