Skip to content

Commit

Permalink
make @timed return NamedTuple (# 34147) (JuliaLang#34149)
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 authored and JeffBezanson committed Jan 8, 2020
1 parent 83cd2c1 commit 41501a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ New library features

Standard library changes
------------------------

* The `@timed` macro now returns a `NamedTuple` ([#34149])

#### LinearAlgebra
* The BLAS submodule now supports the level-2 BLAS subroutine `hpmv!` ([#34211]).
Expand Down
17 changes: 10 additions & 7 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,26 @@ See also [`@time`](@ref), [`@timev`](@ref), [`@elapsed`](@ref), and
[`@allocated`](@ref).
```julia-repl
julia> val, t, bytes, gctime, memallocs = @timed rand(10^6);
julia> stats = @timed rand(10^6);
julia> t
julia> stats.time
0.006634834
julia> bytes
julia> stats.bytes
8000256
julia> gctime
julia> stats.gctime
0.0055765
julia> fieldnames(typeof(memallocs))
julia> propertynames(stats.gcstats)
(:allocd, :malloc, :realloc, :poolalloc, :bigalloc, :freecall, :total_time, :pause, :full_sweep)
julia> memallocs.total_time
julia> stats.gcstats.total_time
5576500
```
!!! compat "Julia 1.5"
The return type of this macro was changed from `Tuple` to `NamedTuple` in Julia 1.5.
"""
macro timed(ex)
quote
Expand All @@ -308,7 +311,7 @@ macro timed(ex)
local val = $(esc(ex))
elapsedtime = time_ns() - elapsedtime
local diff = GC_Diff(gc_num(), stats)
val, elapsedtime/1e9, diff.allocd, diff.total_time/1e9, diff
(value=val, time=elapsedtime/1e9, bytes=diff.allocd, gctime=diff.total_time/1e9, gcstats=diff)
end
end

Expand Down
15 changes: 12 additions & 3 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,18 @@ let t = @elapsed 1+1
end

let
val, t = @timed sin(1)
@test val == sin(1)
@test isa(t, Real) && t >= 0
stats = @timed sin(1)
@test stats.value == sin(1)
@test isa(stats.time, Real) && stats.time >= 0

# The return type of gcstats was changed in Julia 1.4 (# 34147)
# Test that the 1.0 API still works
val, t, bytes, gctime, gcstats = stats
@test val === stats.value
@test t === stats.time
@test bytes === stats.bytes
@test gctime === stats.gctime
@test gcstats === stats.gcstats
end

# problem after #11801 - at global scope
Expand Down

0 comments on commit 41501a5

Please sign in to comment.