Skip to content

Commit

Permalink
Add ballocated macro (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox authored Feb 19, 2020
1 parent 51d22cc commit 688bae9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/BenchmarkTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ include("execution.jl")

export tune!,
warmup,
@ballocated,
@benchmark,
@benchmarkable,
@belapsed,
Expand Down
19 changes: 18 additions & 1 deletion src/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ end
######################

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

"""
Expand All @@ -367,6 +367,23 @@ macro belapsed(args...)
end)
end

"""
@ballocated expression [other parameters...]
Similar to the `@allocated` macro included with Julia,
this returns the number of bytes allocated when executing
a given expression. It uses the `@benchmark`
macro, however, and accepts all of the same additional
parameters as `@benchmark`. The returned allocations
correspond to the trial with the *minimum* elapsed time measured
during the benchmark.
"""
macro ballocated(args...)
return esc(quote
$BenchmarkTools.memory($BenchmarkTools.minimum($BenchmarkTools.@benchmark $(args...)))
end)
end

"""
@btime expression [other parameters...]
Expand Down
4 changes: 4 additions & 0 deletions test/ExecutionTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ tune!(b)
@test @belapsed(sin($(foo.x)), evals=3, samples=10, setup=(foo.x = 0)) < 1
@test @belapsed(sin(0)) < 1

@test @ballocated(sin($(foo.x)), evals=3, samples=10, setup=(foo.x = 0)) == 0
@test @ballocated(sin(0)) == 0
@test @ballocated(Ref(1)) == 2*sizeof(Int) # 1 for the pointer, 1 for content

let fname = tempname()
try
ret = open(fname, "w") do f
Expand Down

0 comments on commit 688bae9

Please sign in to comment.