Skip to content

Commit 9124234

Browse files
print @time msg into print buffer (#50665)
1 parent 6f6439e commit 9124234

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

base/timing.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,14 @@ function format_bytes(bytes; binary=true) # also used by InteractiveUtils
139139
end
140140
end
141141

142-
function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, compile_time=0, recompile_time=0, newline=false, _lpad=true)
142+
function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, compile_time=0, recompile_time=0, newline=false; msg::Union{String,Nothing}=nothing)
143143
timestr = Ryu.writefixed(Float64(elapsedtime/1e9), 6)
144144
str = sprint() do io
145-
_lpad && print(io, length(timestr) < 10 ? (" "^(10 - length(timestr))) : "")
145+
if msg isa String
146+
print(io, msg, ": ")
147+
else
148+
print(io, length(timestr) < 10 ? (" "^(10 - length(timestr))) : "")
149+
end
146150
print(io, timestr, " seconds")
147151
parens = bytes != 0 || allocs != 0 || gctime > 0 || compile_time > 0
148152
parens && print(io, " (")
@@ -179,11 +183,11 @@ function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, compile_ti
179183
nothing
180184
end
181185

182-
function timev_print(elapsedtime, diff::GC_Diff, compile_times, _lpad)
186+
function timev_print(elapsedtime, diff::GC_Diff, compile_times; msg::Union{String,Nothing}=nothing)
183187
allocs = gc_alloc_count(diff)
184188
compile_time = first(compile_times)
185189
recompile_time = last(compile_times)
186-
time_print(stdout, elapsedtime, diff.allocd, diff.total_time, allocs, compile_time, recompile_time, true, _lpad)
190+
time_print(stdout, elapsedtime, diff.allocd, diff.total_time, allocs, compile_time, recompile_time, true; msg)
187191
padded_nonzero_print(elapsedtime, "elapsed time (ns)")
188192
padded_nonzero_print(diff.total_time, "gc time (ns)")
189193
padded_nonzero_print(diff.allocd, "bytes allocated")
@@ -282,9 +286,7 @@ macro time(msg, ex)
282286
)
283287
local diff = GC_Diff(gc_num(), stats)
284288
local _msg = $(esc(msg))
285-
local has_msg = !isnothing(_msg)
286-
has_msg && print(_msg, ": ")
287-
time_print(stdout, elapsedtime, diff.allocd, diff.total_time, gc_alloc_count(diff), first(compile_elapsedtimes), last(compile_elapsedtimes), true, !has_msg)
289+
time_print(stdout, elapsedtime, diff.allocd, diff.total_time, gc_alloc_count(diff), first(compile_elapsedtimes), last(compile_elapsedtimes), true; msg=_msg)
288290
val
289291
end
290292
end
@@ -366,9 +368,7 @@ macro timev(msg, ex)
366368
)
367369
local diff = GC_Diff(gc_num(), stats)
368370
local _msg = $(esc(msg))
369-
local has_msg = !isnothing(_msg)
370-
has_msg && print(_msg, ": ")
371-
timev_print(elapsedtime, diff, compile_elapsedtimes, !has_msg)
371+
timev_print(elapsedtime, diff, compile_elapsedtimes; msg=_msg)
372372
val
373373
end
374374
end

0 commit comments

Comments
 (0)