Skip to content

Commit dd60724

Browse files
committed
docstring + tests
1 parent 128f97f commit dd60724

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
@@ -550,10 +550,26 @@ this executes an expression, printing the time
550550
it took to execute and the memory allocated before
551551
returning the value of the expression.
552552
553-
Unlike `@time`, it uses the `@benchmark`
554-
macro, and accepts all of the same additional
555-
parameters as `@benchmark`. The printed time
556-
is the *minimum* elapsed time measured during the benchmark.
553+
However, it uses the [`@benchmark`](@ref) to evaluate
554+
`expression` many times, and accepts all of the same
555+
additional parameters as `@benchmark`. It prints both
556+
the minimum and the mean elapsed time, plus information
557+
about allocations (if any) and time spent on
558+
memory garbage collection (GC).
559+
560+
# Examples
561+
```
562+
julia> @btime log(x[]) setup=(x=Ref(2.0))
563+
min 3.666 ns, mean 3.772 ns (0 allocations)
564+
0.6931471805599453
565+
566+
julia> @btime sum(log, \$(fill(2.0, 1000)))
567+
min 3.391 μs, mean 3.441 μs (0 allocations)
568+
693.1471805599322
569+
570+
julia> @btime rand(1000);
571+
min 724.432 ns, mean 1.462 μs (1 allocation, 7.94 KiB. GC mean 352 ns, 24.11%)
572+
```
557573
"""
558574
macro btime(args...)
559575
_, 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
@@ -201,6 +201,7 @@ str = String(take!(io))
201201

202202
let fname = tempname()
203203
try
204+
# simple function, zero allocations
204205
ret = open(fname, "w") do f
205206
redirect_stdout(f) do
206207
x = 1
@@ -212,7 +213,21 @@ let fname = tempname()
212213
end
213214
s = read(fname, String)
214215
try
215-
@test occursin(r"[0-9.]+ \w*s \([0-9]* allocations?: [0-9]+ bytes\)", s)
216+
@test occursin(r"min [0-9.]+ \w*s, mean [0-9.]+ \w*s \(0 allocations\)", s)
217+
catch
218+
println(stderr, "@btime output didn't match ", repr(s))
219+
rethrow()
220+
end
221+
# function which allocates
222+
ret2 = open(fname, "w") do f
223+
redirect_stdout(f) do
224+
y = @btime(sum(log, ones(100)))
225+
@test y 0
226+
end
227+
end
228+
s2 = read(fname, String)
229+
try
230+
@test occursin(r", mean [0-9.]+ \w*s \([0-9]* allocations?, [0-9]+ bytes. GC mean [0-9.]+ \w*s,", s2)
216231
catch
217232
println(stderr, "@btime output didn't match ", repr(s))
218233
rethrow()

0 commit comments

Comments
 (0)