Skip to content

Commit 688bae9

Browse files
authored
Add ballocated macro (#157)
1 parent 51d22cc commit 688bae9

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/BenchmarkTools.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ include("execution.jl")
6060

6161
export tune!,
6262
warmup,
63+
@ballocated,
6364
@benchmark,
6465
@benchmarkable,
6566
@belapsed,

src/execution.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ end
348348
######################
349349

350350
# These macros provide drop-in replacements for the
351-
# Base.@time and Base.@elapsed macros, which use
351+
# Base.@time, Base.@elapsed macros and Base.@allocated, which use
352352
# @benchmark but yield only the minimum time.
353353

354354
"""
@@ -367,6 +367,23 @@ macro belapsed(args...)
367367
end)
368368
end
369369

370+
"""
371+
@ballocated expression [other parameters...]
372+
373+
Similar to the `@allocated` macro included with Julia,
374+
this returns the number of bytes allocated when executing
375+
a given expression. It uses the `@benchmark`
376+
macro, however, and accepts all of the same additional
377+
parameters as `@benchmark`. The returned allocations
378+
correspond to the trial with the *minimum* elapsed time measured
379+
during the benchmark.
380+
"""
381+
macro ballocated(args...)
382+
return esc(quote
383+
$BenchmarkTools.memory($BenchmarkTools.minimum($BenchmarkTools.@benchmark $(args...)))
384+
end)
385+
end
386+
370387
"""
371388
@btime expression [other parameters...]
372389

test/ExecutionTests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ tune!(b)
154154
@test @belapsed(sin($(foo.x)), evals=3, samples=10, setup=(foo.x = 0)) < 1
155155
@test @belapsed(sin(0)) < 1
156156

157+
@test @ballocated(sin($(foo.x)), evals=3, samples=10, setup=(foo.x = 0)) == 0
158+
@test @ballocated(sin(0)) == 0
159+
@test @ballocated(Ref(1)) == 2*sizeof(Int) # 1 for the pointer, 1 for content
160+
157161
let fname = tempname()
158162
try
159163
ret = open(fname, "w") do f

0 commit comments

Comments
 (0)