-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 @allocations
macro for getting number of allocations
#47367
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1018,6 +1018,7 @@ export | |
@timev, | ||
@elapsed, | ||
@allocated, | ||
@allocs, | ||
|
||
# tasks | ||
@sync, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,8 +218,8 @@ In some cases the system will look inside the `@time` expression and compile som | |
called code before execution of the top-level expression begins. When that happens, some | ||
compilation time will not be counted. To include this time you can run `@time @eval ...`. | ||
|
||
See also [`@showtime`](@ref), [`@timev`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), and | ||
[`@allocated`](@ref). | ||
See also [`@showtime`](@ref), [`@timev`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), | ||
[`@allocated`](@ref), and [`@allocs`](@ref). | ||
|
||
!!! note | ||
For more serious benchmarking, consider the `@btime` macro from the BenchmarkTools.jl | ||
|
@@ -318,8 +318,8 @@ Optionally provide a description string to print before the time report. | |
!!! compat "Julia 1.8" | ||
The option to add a description was introduced in Julia 1.8. | ||
|
||
See also [`@time`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), and | ||
[`@allocated`](@ref). | ||
See also [`@time`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), | ||
[`@allocated`](@ref), and [`@allocs`](@ref). | ||
|
||
```julia-repl | ||
julia> x = rand(10,10); | ||
|
@@ -379,7 +379,7 @@ called code before execution of the top-level expression begins. When that happe | |
compilation time will not be counted. To include this time you can run `@elapsed @eval ...`. | ||
|
||
See also [`@time`](@ref), [`@timev`](@ref), [`@timed`](@ref), | ||
and [`@allocated`](@ref). | ||
[`@allocated`](@ref), and [`@allocs`](@ref). | ||
|
||
```julia-repl | ||
julia> @elapsed sleep(0.3) | ||
|
@@ -410,7 +410,7 @@ end | |
A macro to evaluate an expression, discarding the resulting value, instead returning the | ||
total number of bytes allocated during evaluation of the expression. | ||
|
||
See also [`@time`](@ref), [`@timev`](@ref), [`@timed`](@ref), | ||
See also [`@allocs`](@ref), [`@time`](@ref), [`@timev`](@ref), [`@timed`](@ref), | ||
and [`@elapsed`](@ref). | ||
|
||
```julia-repl | ||
|
@@ -430,6 +430,33 @@ macro allocated(ex) | |
end | ||
end | ||
|
||
""" | ||
@allocs | ||
|
||
A macro to evaluate an expression, discard the resulting value, and instead return the | ||
total number of allocations during evaluation of the expression. | ||
|
||
See also [`@allocated`](@ref), [`@time`](@ref), [`@timev`](@ref), [`@timed`](@ref), | ||
and [`@elapsed`](@ref). | ||
|
||
```julia-repl | ||
julia> @allocs rand(10^6) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i just copied the example from |
||
2 | ||
``` | ||
|
||
!!! compat "Julia 1.9" | ||
This macro was added in Julia 1.9. | ||
nickrobinson251 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
macro allocs(ex) | ||
quote | ||
Experimental.@force_compile | ||
nickrobinson251 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
local stats = Base.gc_num() | ||
$(esc(ex)) | ||
local diff = Base.GC_Diff(Base.gc_num(), stats) | ||
Base.gc_alloc_count(diff) | ||
end | ||
end | ||
|
||
""" | ||
@timed | ||
|
||
|
@@ -441,8 +468,8 @@ In some cases the system will look inside the `@timed` expression and compile so | |
called code before execution of the top-level expression begins. When that happens, some | ||
compilation time will not be counted. To include this time you can run `@timed @eval ...`. | ||
|
||
See also [`@time`](@ref), [`@timev`](@ref), [`@elapsed`](@ref), and | ||
[`@allocated`](@ref). | ||
See also [`@time`](@ref), [`@timev`](@ref), [`@elapsed`](@ref), | ||
[`@allocated`](@ref), and [`@allocs`](@ref). | ||
|
||
```julia-repl | ||
julia> stats = @timed rand(10^6); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1234,4 +1234,8 @@ end | |
|
||
@testset "Base/timing.jl" begin | ||
@test Base.jit_total_bytes() >= 0 | ||
|
||
# sanity check `@allocs` returns what we expect in some very simple cases | ||
@test (@alloc "a") == 0 | ||
@test (@alloc "a" * "b") == 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i didn't see any tests for |
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name suggestions welcome, this just felt obvious/easy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
@nallocs
similar tondigits
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to
@allocations
for now, as it leads to a less bad name in BenchmarkTools.jl (where the corresponding name would be@ballocations
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂 "ballocs"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, would have been unfortunate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.