@@ -175,25 +175,31 @@ macro __tryfinally(ex, fin)
175175end
176176
177177"""
178- @time
178+ @time expr
179+ @time "description" expr
179180
180181A macro to execute an expression, printing the time it took to execute, the number of
181182allocations, and the total number of bytes its execution caused to be allocated, before
182183returning the value of the expression. Any time spent garbage collecting (gc) or
183184compiling is shown as a percentage.
184185
186+ Optionally provide a description string to print before the time report.
187+
185188In some cases the system will look inside the `@time` expression and compile some of the
186189called code before execution of the top-level expression begins. When that happens, some
187190compilation time will not be counted. To include this time you can run `@time @eval ...`.
188191
189- See also [`@timev`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), and
192+ See also [`@showtime`](@ref), [`@ timev`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), and
190193[`@allocated`](@ref).
191194
192195!!! note
193196 For more serious benchmarking, consider the `@btime` macro from the BenchmarkTools.jl
194197 package which among other things evaluates the function multiple times in order to
195198 reduce noise.
196199
200+ !!! compat "Julia 1.8"
201+ The option to add a description was introduced in Julia 1.8.
202+
197203```julia-repl
198204julia> x = rand(10,10);
199205
@@ -209,9 +215,24 @@ julia> @time begin
209215 end
210216 0.301395 seconds (8 allocations: 336 bytes)
2112172
218+
219+ julia> @time "A one second sleep" sleep(1)
220+ A one second sleep -> 1.005750 seconds (5 allocations: 144 bytes)
221+
222+ julia> for loop in 1:3
223+ @time loop sleep(1)
224+ 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)
212228```
213229"""
214230macro time (ex)
231+ quote
232+ @time nothing $ (esc (ex))
233+ end
234+ end
235+ macro time (msg, ex)
215236 quote
216237 Experimental. @force_compile
217238 local stats = gc_num ()
@@ -222,18 +243,46 @@ macro time(ex)
222243 compile_elapsedtime = cumulative_compile_time_ns_after () - compile_elapsedtime)
223244 )
224245 local diff = GC_Diff (gc_num (), stats)
246+ isnothing ($ (esc (msg))) || print ($ (esc (msg)), " ->" )
225247 time_print (elapsedtime, diff. allocd, diff. total_time, gc_alloc_count (diff), compile_elapsedtime, true )
226248 val
227249 end
228250end
229251
230252"""
231- @timev
253+ @showtime expr
254+
255+ Like `@time` but also prints the expression being evaluated for reference.
256+
257+ !!! compat "Julia 1.8"
258+ This macro was added in Julia 1.8.
259+
260+ See also [`@time`](@ref).
261+
262+ ```julia-repl
263+ julia> @showtime sleep(1)
264+ sleep(1) -> 1.002164 seconds (4 allocations: 128 bytes)
265+ ```
266+ """
267+ macro showtime (ex)
268+ quote
269+ @time $ (sprint (show_unquoted,ex)) $ (esc (ex))
270+ end
271+ end
272+
273+ """
274+ @timev expr
275+ @timev "description" expr
232276
233277This is a verbose version of the `@time` macro. It first prints the same information as
234278`@time`, then any non-zero memory allocation counters, and then returns the value of the
235279expression.
236280
281+ Optionally provide a description string to print before the time report.
282+
283+ !!! compat "Julia 1.8"
284+ The option to add a description was introduced in Julia 1.8.
285+
237286See also [`@time`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), and
238287[`@allocated`](@ref).
239288
@@ -259,6 +308,11 @@ pool allocs: 1
259308```
260309"""
261310macro timev (ex)
311+ quote
312+ @timev nothing $ (esc (ex))
313+ end
314+ end
315+ macro timev (msg, ex)
262316 quote
263317 Experimental. @force_compile
264318 local stats = gc_num ()
@@ -269,6 +323,7 @@ macro timev(ex)
269323 compile_elapsedtime = cumulative_compile_time_ns_after () - compile_elapsedtime)
270324 )
271325 local diff = GC_Diff (gc_num (), stats)
326+ isnothing ($ (esc (msg))) || print ($ (esc (msg)), " ->" )
272327 timev_print (elapsedtime, diff, compile_elapsedtime)
273328 val
274329 end
0 commit comments