From 912423456dfe9f54c7454efd252a496aa1ea579f Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Tue, 25 Jul 2023 11:59:28 -0400 Subject: [PATCH] print `@time` msg into print buffer (#50665) --- base/timing.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/base/timing.jl b/base/timing.jl index ce458fafc7c78..bc4bd73d927b2 100644 --- a/base/timing.jl +++ b/base/timing.jl @@ -139,10 +139,14 @@ function format_bytes(bytes; binary=true) # also used by InteractiveUtils end end -function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, compile_time=0, recompile_time=0, newline=false, _lpad=true) +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) timestr = Ryu.writefixed(Float64(elapsedtime/1e9), 6) str = sprint() do io - _lpad && print(io, length(timestr) < 10 ? (" "^(10 - length(timestr))) : "") + if msg isa String + print(io, msg, ": ") + else + print(io, length(timestr) < 10 ? (" "^(10 - length(timestr))) : "") + end print(io, timestr, " seconds") parens = bytes != 0 || allocs != 0 || gctime > 0 || compile_time > 0 parens && print(io, " (") @@ -179,11 +183,11 @@ function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, compile_ti nothing end -function timev_print(elapsedtime, diff::GC_Diff, compile_times, _lpad) +function timev_print(elapsedtime, diff::GC_Diff, compile_times; msg::Union{String,Nothing}=nothing) allocs = gc_alloc_count(diff) compile_time = first(compile_times) recompile_time = last(compile_times) - time_print(stdout, elapsedtime, diff.allocd, diff.total_time, allocs, compile_time, recompile_time, true, _lpad) + time_print(stdout, elapsedtime, diff.allocd, diff.total_time, allocs, compile_time, recompile_time, true; msg) padded_nonzero_print(elapsedtime, "elapsed time (ns)") padded_nonzero_print(diff.total_time, "gc time (ns)") padded_nonzero_print(diff.allocd, "bytes allocated") @@ -282,9 +286,7 @@ macro time(msg, ex) ) local diff = GC_Diff(gc_num(), stats) local _msg = $(esc(msg)) - local has_msg = !isnothing(_msg) - has_msg && print(_msg, ": ") - time_print(stdout, elapsedtime, diff.allocd, diff.total_time, gc_alloc_count(diff), first(compile_elapsedtimes), last(compile_elapsedtimes), true, !has_msg) + time_print(stdout, elapsedtime, diff.allocd, diff.total_time, gc_alloc_count(diff), first(compile_elapsedtimes), last(compile_elapsedtimes), true; msg=_msg) val end end @@ -366,9 +368,7 @@ macro timev(msg, ex) ) local diff = GC_Diff(gc_num(), stats) local _msg = $(esc(msg)) - local has_msg = !isnothing(_msg) - has_msg && print(_msg, ": ") - timev_print(elapsedtime, diff, compile_elapsedtimes, !has_msg) + timev_print(elapsedtime, diff, compile_elapsedtimes; msg=_msg) val end end