You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/index.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ For quick sanity checks, one can use the [`@btime` macro](https://github.com/Jul
33
33
34
34
```julia
35
35
julia>@btimesin(x) setup=(x=rand())
36
-
4.361 ns (0 allocations:0 bytes)
36
+
min 3.041 ns, mean 3.487 ns (0 allocations)
37
37
0.49587200950472454
38
38
```
39
39
@@ -45,16 +45,16 @@ If the expression you want to benchmark depends on external variables, you shoul
45
45
Essentially, any interpolated variable `$x` or expression `$(...)` is "pre-computed" before benchmarking begins:
46
46
47
47
```julia
48
-
julia> A =rand(3,3);
48
+
julia> A =rand(8,8);
49
49
50
50
julia>@btimeinv($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)
52
52
53
-
julia>@btimeinv($(rand(3,3))); # interpolation: the rand(3,3) call occurs before benchmarking
54
-
1.192 μs (10 allocations:2.31 KiB)
53
+
julia>@btimeinv($(rand(8,8))); # interpolation: the rand() call occurs before benchmarking
54
+
min 930.542 ns, mean 1.363 μs (4 allocations, 4.81 KiB)
55
55
56
-
julia>@btimeinv(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>@btimeinv(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)
58
58
```
59
59
60
60
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
63
63
2
64
64
65
65
julia>@btime$a +$b
66
-
0.024 ns (0 allocations:0 bytes)
66
+
min 0.875 ns, mean 0.950 ns (0 allocations)
67
67
3
68
68
```
69
69
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
Copy file name to clipboardExpand all lines: docs/src/manual.md
+10-7Lines changed: 10 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -65,12 +65,15 @@ before returning the value of the expression, while `@belapsed`
65
65
returns the minimum time in seconds.
66
66
67
67
```julia
68
-
julia>@btimesin(1)
69
-
13.612 ns (0 allocations:0 bytes)
70
-
0.8414709848078965
68
+
julia>@btimesin.(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
71
74
72
-
julia>@belapsedsin(1)
73
-
1.3614228456913828e-8
75
+
julia>@belapsedsin.(0:2)
76
+
2.5727911646586344e-8
74
77
```
75
78
76
79
### Benchmark `Parameters`
@@ -280,13 +283,13 @@ julia> a = 1; b = 2
280
283
2
281
284
282
285
julia>@btime$a +$b
283
-
0.024 ns (0 allocations:0 bytes)
286
+
min 0.833 ns, mean 0.944 ns (0 allocations)
284
287
3
285
288
```
286
289
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
0 commit comments