From e17fe81c856268283c6abfd0fc3566bbc9ceacdf Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Fri, 28 Oct 2022 17:16:28 +0100 Subject: [PATCH 1/3] Add `@ballocs` macro for getting number of allocations --- src/BenchmarkTools.jl | 1 + src/execution.jl | 22 +++++++++++++++++++++- test/ExecutionTests.jl | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/BenchmarkTools.jl b/src/BenchmarkTools.jl index 600cb7f4..68b4e164 100644 --- a/src/BenchmarkTools.jl +++ b/src/BenchmarkTools.jl @@ -66,6 +66,7 @@ include("execution.jl") export tune!, warmup, @ballocated, + @ballocs, @benchmark, @benchmarkable, @belapsed, diff --git a/src/execution.jl b/src/execution.jl index e8b1d509..fe0e1d43 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -539,12 +539,14 @@ 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 `@ballocs`. """ macro ballocated(args...) return esc(quote @@ -552,6 +554,24 @@ macro ballocated(args...) end) end +""" + @ballocs expression [other parameters...] + +Similar to the `@allocs` macro included with Julia (v1.9+), +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. + +See also `@ballocated`. +""" +macro ballocs(args...) + return esc(quote + $BenchmarkTools.allocs($BenchmarkTools.minimum($BenchmarkTools.@benchmark $(args...))) + end) +end + """ @btime expression [other parameters...] diff --git a/test/ExecutionTests.jl b/test/ExecutionTests.jl index 4e72f76d..a1a08281 100644 --- a/test/ExecutionTests.jl +++ b/test/ExecutionTests.jl @@ -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 @ballocs(sin($(foo.x)), evals=3, samples=10, setup=(foo.x = 0)) == 0 +@test @ballocs(sin(0)) == 0 +@test @ballocs(Ref(1)) == 1 + let fname = tempname() try ret = open(fname, "w") do f From 91de800d495abdf74b1ea4d4518bc33c14716817 Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Fri, 4 Nov 2022 16:03:43 +0000 Subject: [PATCH 2/3] Rename to `@ballocations` --- src/BenchmarkTools.jl | 2 +- src/execution.jl | 6 +++--- test/ExecutionTests.jl | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/BenchmarkTools.jl b/src/BenchmarkTools.jl index 68b4e164..72987972 100644 --- a/src/BenchmarkTools.jl +++ b/src/BenchmarkTools.jl @@ -66,7 +66,7 @@ include("execution.jl") export tune!, warmup, @ballocated, - @ballocs, + @ballocations, @benchmark, @benchmarkable, @belapsed, diff --git a/src/execution.jl b/src/execution.jl index fe0e1d43..a1965f6c 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -546,7 +546,7 @@ parameters as `@benchmark`. The returned allocations correspond to the trial with the *minimum* elapsed time measured during the benchmark. -See also `@ballocs`. +See also `@ballocations`. """ macro ballocated(args...) return esc(quote @@ -555,7 +555,7 @@ macro ballocated(args...) end """ - @ballocs expression [other parameters...] + @ballocations expression [other parameters...] Similar to the `@allocs` macro included with Julia (v1.9+), this returns the *number of allocations* when executing @@ -566,7 +566,7 @@ the *minimum* elapsed time measured during the benchmark. See also `@ballocated`. """ -macro ballocs(args...) +macro ballocations(args...) return esc(quote $BenchmarkTools.allocs($BenchmarkTools.minimum($BenchmarkTools.@benchmark $(args...))) end) diff --git a/test/ExecutionTests.jl b/test/ExecutionTests.jl index a1a08281..ce812819 100644 --- a/test/ExecutionTests.jl +++ b/test/ExecutionTests.jl @@ -225,9 +225,9 @@ str = String(take!(io)) @test @ballocated(sin(0)) == 0 @test @ballocated(Ref(1)) == 2*sizeof(Int) # 1 for the pointer, 1 for content -@test @ballocs(sin($(foo.x)), evals=3, samples=10, setup=(foo.x = 0)) == 0 -@test @ballocs(sin(0)) == 0 -@test @ballocs(Ref(1)) == 1 +@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 From 3fc7742482d61682ad7afee82947818459b20835 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:46:45 +0200 Subject: [PATCH 3/3] Update src/execution.jl --- src/execution.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/execution.jl b/src/execution.jl index a1965f6c..413d99b2 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -557,7 +557,7 @@ end """ @ballocations expression [other parameters...] -Similar to the `@allocs` macro included with Julia (v1.9+), +Similar to the `@allocations` macro included with Julia (v1.9+), 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`.