Skip to content

Commit 84b424d

Browse files
authored
update example to use best practices for parameter caching (#42)
* update docs to use BenchmarkTools serialization wrapper * update example to use best practices for parameter caching
1 parent d7e9223 commit 84b424d

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.jl.cov
22
*.jl.*.cov
33
*.jl.mem
4+
benchmark/params.jld

benchmark/benchmarks.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BenchmarkTools
2-
using JLD
32

43
# Define a parent BenchmarkGroup to contain our suite
54
const suite = BenchmarkGroup()
@@ -22,8 +21,14 @@ for f in (sin, cos, tan)
2221
end
2322
end
2423

25-
# Load the suite's cached parameters as part of including the file. This is much
26-
# faster and more reliable than re-tuning `suite` every time the file is included
27-
paramspath = joinpath(Pkg.dir("BenchmarkTools"), "benchmark", "params.jld")
28-
# tune!(suite); JLD.save(paramspath, "suite", params(suite));
29-
loadparams!(suite, JLD.load(paramspath, "suite"), :evals, :samples);
24+
# If a cache of tuned parameters already exists, use it, otherwise, tune and cache
25+
# the benchmark parameters. Reusing cached parameters is faster and more reliable
26+
# than re-tuning `suite` every time the file is included.
27+
paramspath = joinpath(dirname(@__FILE__), "params.jld")
28+
29+
if isfile(paramspath)
30+
loadparams!(suite, BenchmarkTools.load(paramspath, "suite"), :evals, :samples);
31+
else
32+
tune!(suite)
33+
BenchmarkTools.save(paramspath, "suite", params(suite));
34+
end

benchmark/params.jld

-25.3 KB
Binary file not shown.

doc/manual.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -918,17 +918,17 @@ BenchmarkTools.BenchmarkGroup:
918918
# tune the suite to configure benchmark parameters
919919
julia> tune!(suite);
920920

921-
julia> using JLD
922-
923-
# save the suite's parameters using JLD
924-
julia> JLD.save("params.jld", "suite", params(suite));
921+
# save the suite's parameters using a thin wrapper
922+
# over JLD (this wrapper maintains compatibility
923+
# across BenchmarkTools versions)
924+
julia> BenchmarkTools.save("params.jld", "suite", params(suite));
925925
```
926926

927927
Now, instead of tuning `suite` every time we load the benchmarks in a new Julia session, we can simply load the parameters in the JLD file using the `loadparams!` function:
928928

929929
```julia
930930
# syntax is loadparams!(group, paramsgroup, fields...)
931-
julia> loadparams!(suite, JLD.load("params.jld", "suite"), :evals, :samples);
931+
julia> loadparams!(suite, BenchmarkTools.load("params.jld", "suite"), :evals, :samples);
932932
```
933933

934934
Caching parameters in this manner leads to a far shorter turnaround time, and more importantly, much more consistent results.

doc/reference.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ end
183183

184184
Return an iterator over `x`'s leaf index/value pairs. Relevant manual documentation can be found [here](manual.md#indexing-into-a-benchmarkgroup-using-a-vector).
185185

186+
##### `save(filename, args...)`
187+
188+
Not exported. This function is a thin wrapper around a call to `JLD.save(filename, args...)` that maintains compatibility between BenchmarkTools versions.
189+
190+
##### `load(filename, args...)`
191+
192+
Not exported. This function is a thin wrapper around a call to `JLD.load(filename, args...)` that maintains compatibility between BenchmarkTools versions.
193+
186194
##### `loadparams!(x::Parameters, y::Parameters, fields...)`
187195
##### `loadparams!(x::Benchmark, y::Parameters, fields...)`
188196
##### `loadparams!(x::BenchmarkGroup, y::BenchmarkGroup, fields...)`

0 commit comments

Comments
 (0)