Skip to content

Commit

Permalink
print @time msg into print buffer (#50665)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9124234)
  • Loading branch information
IanButterworth authored and KristofferC committed Jul 28, 2023
1 parent ea34b13 commit 6ea6ddd
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions base/timing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,14 @@ function format_bytes(bytes) # 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, " (")
Expand Down Expand Up @@ -176,11 +180,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")
Expand Down Expand Up @@ -279,9 +283,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
Expand Down Expand Up @@ -363,9 +365,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
Expand Down

0 comments on commit 6ea6ddd

Please sign in to comment.