Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @ballocations macro for getting number of allocations #292

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/BenchmarkTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ include("execution.jl")
export tune!,
warmup,
@ballocated,
@ballocations,
@benchmark,
@benchmarkable,
@belapsed,
Expand Down
22 changes: 21 additions & 1 deletion src/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,19 +539,39 @@ end
@ballocated expression [other parameters...]

Similar to the `@allocated` macro included with Julia,
this returns the number of bytes allocated when executing
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.

See also `@ballocations`.
"""
macro ballocated(args...)
return esc(quote
$BenchmarkTools.memory($BenchmarkTools.minimum($BenchmarkTools.@benchmark $(args...)))
end)
end

"""
@ballocations expression [other parameters...]

Similar to the `@allocs` macro included with Julia (v1.9+),
gdalle marked this conversation as resolved.
Show resolved Hide resolved
this returns the *number of allocations* 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set a default value evals=1 samples=2 for this one? And explain it in the docstring?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then again it would be incoherent with the other @bstuff macros so I'm torn

See also `@ballocated`.
"""
macro ballocations(args...)
return esc(quote
$BenchmarkTools.allocs($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 @@ -225,6 +225,10 @@ str = String(take!(io))
@test @ballocated(sin(0)) == 0
@test @ballocated(Ref(1)) == 2*sizeof(Int) # 1 for the pointer, 1 for content

@test @ballocations(sin($(foo.x)), evals=3, samples=10, setup=(foo.x = 0)) == 0
@test @ballocations(sin(0)) == 0
@test @ballocations(Ref(1)) == 1

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