Skip to content

Commit 2d9869a

Browse files
committed
docstring + tests
1 parent 9b41541 commit 2d9869a

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

src/execution.jl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,26 @@ this executes an expression, printing the time
554554
it took to execute and the memory allocated before
555555
returning the value of the expression.
556556
557-
Unlike `@time`, it uses the `@benchmark`
558-
macro, and accepts all of the same additional
559-
parameters as `@benchmark`. The printed time
560-
is the *minimum* elapsed time measured during the benchmark.
557+
However, it uses the [`@benchmark`](@ref) to evaluate
558+
`expression` many times, and accepts all of the same
559+
additional parameters as `@benchmark`. It prints both
560+
the minimum and the mean elapsed time, plus information
561+
about allocations (if any) and time spent on
562+
memory garbage collection (GC).
563+
564+
# Examples
565+
```
566+
julia> @btime log(x[]) setup=(x=Ref(2.0))
567+
min 3.666 ns, mean 3.772 ns (0 allocations)
568+
0.6931471805599453
569+
570+
julia> @btime sum(log, \$(fill(2.0, 1000)))
571+
min 3.391 μs, mean 3.441 μs (0 allocations)
572+
693.1471805599322
573+
574+
julia> @btime rand(1000);
575+
min 724.432 ns, mean 1.462 μs (1 allocation, 7.94 KiB. GC mean 352 ns, 24.11%)
576+
```
561577
"""
562578
macro btime(args...)
563579
_, params = prunekwargs(args...)

src/trials.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function prettytime(t; short=false)
264264
value, units = t / 1e9, "s"
265265
end
266266
if short
267-
string(@sprintf("%.0f", value), " ", units)
267+
string(@sprintf("%.0f", value), " ", units) # "%.3g" also OK, always 3 numbers
268268
else
269269
string(@sprintf("%.3f", value), " ", units)
270270
end

test/ExecutionTests.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ str = String(take!(io))
227227

228228
let fname = tempname()
229229
try
230+
# simple function, zero allocations
230231
ret = open(fname, "w") do f
231232
redirect_stdout(f) do
232233
x = 1
@@ -238,7 +239,21 @@ let fname = tempname()
238239
end
239240
s = read(fname, String)
240241
try
241-
@test occursin(r"[0-9.]+ \w*s \([0-9]* allocations?: [0-9]+ bytes\)", s)
242+
@test occursin(r"min [0-9.]+ \w*s, mean [0-9.]+ \w*s \(0 allocations\)", s)
243+
catch
244+
println(stderr, "@btime output didn't match ", repr(s))
245+
rethrow()
246+
end
247+
# function which allocates
248+
ret2 = open(fname, "w") do f
249+
redirect_stdout(f) do
250+
y = @btime(sum(log, ones(100)))
251+
@test y 0
252+
end
253+
end
254+
s2 = read(fname, String)
255+
try
256+
@test occursin(r", mean [0-9.]+ \w*s \([0-9]* allocations?, [0-9]+ bytes. GC mean [0-9.]+ \w*s,", s2)
242257
catch
243258
println(stderr, "@btime output didn't match ", repr(s))
244259
rethrow()

0 commit comments

Comments
 (0)