Skip to content

Commit 03b16ab

Browse files
Use : for time and showtime msg formatting (#43255)
Co-authored-by: Fredrik Ekre <[email protected]>
1 parent ed9ad60 commit 03b16ab

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

base/timing.jl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ function format_bytes(bytes) # also used by InteractiveUtils
115115
end
116116
end
117117

118-
function time_print(elapsedtime, bytes=0, gctime=0, allocs=0, compile_time=0, newline=false)
118+
function time_print(elapsedtime, bytes=0, gctime=0, allocs=0, compile_time=0, newline=false, _lpad=true)
119119
timestr = Ryu.writefixed(Float64(elapsedtime/1e9), 6)
120120
str = sprint() do io
121-
print(io, length(timestr) < 10 ? (" "^(10 - length(timestr))) : "")
121+
_lpad && print(io, length(timestr) < 10 ? (" "^(10 - length(timestr))) : "")
122122
print(io, timestr, " seconds")
123123
parens = bytes != 0 || allocs != 0 || gctime > 0 || compile_time > 0
124124
parens && print(io, " (")
@@ -149,9 +149,9 @@ function time_print(elapsedtime, bytes=0, gctime=0, allocs=0, compile_time=0, ne
149149
nothing
150150
end
151151

152-
function timev_print(elapsedtime, diff::GC_Diff, compile_time)
152+
function timev_print(elapsedtime, diff::GC_Diff, compile_time, _lpad)
153153
allocs = gc_alloc_count(diff)
154-
time_print(elapsedtime, diff.allocd, diff.total_time, allocs, compile_time, true)
154+
time_print(elapsedtime, diff.allocd, diff.total_time, allocs, compile_time, true, _lpad)
155155
print("elapsed time (ns): $elapsedtime\n")
156156
padded_nonzero_print(diff.total_time, "gc time (ns)")
157157
padded_nonzero_print(diff.allocd, "bytes allocated")
@@ -217,14 +217,14 @@ julia> @time begin
217217
2
218218
219219
julia> @time "A one second sleep" sleep(1)
220-
A one second sleep -> 1.005750 seconds (5 allocations: 144 bytes)
220+
A one second sleep: 1.005750 seconds (5 allocations: 144 bytes)
221221
222222
julia> for loop in 1:3
223223
@time loop sleep(1)
224224
end
225-
1 -> 1.006760 seconds (5 allocations: 144 bytes)
226-
2 -> 1.001263 seconds (5 allocations: 144 bytes)
227-
3 -> 1.003676 seconds (5 allocations: 144 bytes)
225+
1: 1.006760 seconds (5 allocations: 144 bytes)
226+
2: 1.001263 seconds (5 allocations: 144 bytes)
227+
3: 1.003676 seconds (5 allocations: 144 bytes)
228228
```
229229
"""
230230
macro time(ex)
@@ -243,8 +243,10 @@ macro time(msg, ex)
243243
compile_elapsedtime = cumulative_compile_time_ns_after() - compile_elapsedtime)
244244
)
245245
local diff = GC_Diff(gc_num(), stats)
246-
isnothing($(esc(msg))) || print($(esc(msg)), " ->")
247-
time_print(elapsedtime, diff.allocd, diff.total_time, gc_alloc_count(diff), compile_elapsedtime, true)
246+
local _msg = $(esc(msg))
247+
local has_msg = !isnothing(_msg)
248+
has_msg && print(_msg, ": ")
249+
time_print(elapsedtime, diff.allocd, diff.total_time, gc_alloc_count(diff), compile_elapsedtime, true, !has_msg)
248250
val
249251
end
250252
end
@@ -261,7 +263,7 @@ See also [`@time`](@ref).
261263
262264
```julia-repl
263265
julia> @showtime sleep(1)
264-
sleep(1) -> 1.002164 seconds (4 allocations: 128 bytes)
266+
sleep(1): 1.002164 seconds (4 allocations: 128 bytes)
265267
```
266268
"""
267269
macro showtime(ex)
@@ -323,8 +325,10 @@ macro timev(msg, ex)
323325
compile_elapsedtime = cumulative_compile_time_ns_after() - compile_elapsedtime)
324326
)
325327
local diff = GC_Diff(gc_num(), stats)
326-
isnothing($(esc(msg))) || print($(esc(msg)), " ->")
327-
timev_print(elapsedtime, diff, compile_elapsedtime)
328+
local _msg = $(esc(msg))
329+
local has_msg = !isnothing(_msg)
330+
has_msg && print(_msg, ": ")
331+
timev_print(elapsedtime, diff, compile_elapsedtime, !has_msg)
328332
val
329333
end
330334
end

0 commit comments

Comments
 (0)