Skip to content

Commit ec224d4

Browse files
jw3126KristofferC
authored andcommitted
make @timed return NamedTuple (# 34147) (#34149)
1 parent 2358206 commit ec224d4

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ New library features
2727

2828
Standard library changes
2929
------------------------
30-
30+
* The `@timed` macro now returns a `NamedTuple` ([#34149])
3131

3232
#### LinearAlgebra
3333
* The BLAS submodule now supports the level-2 BLAS subroutine `hpmv!` ([#34211]).

base/util.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,23 +282,26 @@ See also [`@time`](@ref), [`@timev`](@ref), [`@elapsed`](@ref), and
282282
[`@allocated`](@ref).
283283
284284
```julia-repl
285-
julia> val, t, bytes, gctime, memallocs = @timed rand(10^6);
285+
julia> stats = @timed rand(10^6);
286286
287-
julia> t
287+
julia> stats.time
288288
0.006634834
289289
290-
julia> bytes
290+
julia> stats.bytes
291291
8000256
292292
293-
julia> gctime
293+
julia> stats.gctime
294294
0.0055765
295295
296-
julia> fieldnames(typeof(memallocs))
296+
julia> propertynames(stats.gcstats)
297297
(:allocd, :malloc, :realloc, :poolalloc, :bigalloc, :freecall, :total_time, :pause, :full_sweep)
298298
299-
julia> memallocs.total_time
299+
julia> stats.gcstats.total_time
300300
5576500
301301
```
302+
303+
!!! compat "Julia 1.5"
304+
The return type of this macro was changed from `Tuple` to `NamedTuple` in Julia 1.5.
302305
"""
303306
macro timed(ex)
304307
quote
@@ -308,7 +311,7 @@ macro timed(ex)
308311
local val = $(esc(ex))
309312
elapsedtime = time_ns() - elapsedtime
310313
local diff = GC_Diff(gc_num(), stats)
311-
val, elapsedtime/1e9, diff.allocd, diff.total_time/1e9, diff
314+
(value=val, time=elapsedtime/1e9, bytes=diff.allocd, gctime=diff.total_time/1e9, gcstats=diff)
312315
end
313316
end
314317

test/misc.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,18 @@ let t = @elapsed 1+1
158158
end
159159

160160
let
161-
val, t = @timed sin(1)
162-
@test val == sin(1)
163-
@test isa(t, Real) && t >= 0
161+
stats = @timed sin(1)
162+
@test stats.value == sin(1)
163+
@test isa(stats.time, Real) && stats.time >= 0
164+
165+
# The return type of gcstats was changed in Julia 1.4 (# 34147)
166+
# Test that the 1.0 API still works
167+
val, t, bytes, gctime, gcstats = stats
168+
@test val === stats.value
169+
@test t === stats.time
170+
@test bytes === stats.bytes
171+
@test gctime === stats.gctime
172+
@test gcstats === stats.gcstats
164173
end
165174

166175
# problem after #11801 - at global scope

0 commit comments

Comments
 (0)