@@ -172,15 +172,37 @@ Base.@deprecate get_defaultimer get_defaulttimer
172172
173173# Macro
174174macro timeit (args... )
175- return timer_expr (__module__, false , args... )
175+ blocks = timer_expr (__module__, false , args... )
176+ if blocks isa Expr
177+ blocks
178+ else
179+ Expr (:block ,
180+ blocks[1 ], # the timing setup
181+ Expr (:tryfinally ,
182+ :($ (esc (args[end ]))), # the user expr
183+ :($ (blocks[2 ])) # the timing finally
184+ )
185+ )
186+ end
176187end
177188
178189macro timeit_debug (args... )
179190 if ! isdefined (__module__, :timeit_debug_enabled )
180191 Core. eval (__module__, :(timeit_debug_enabled () = false ))
181192 end
182193
183- return timer_expr (__module__, true , args... )
194+ blocks = timer_expr (__module__, true , args... )
195+ if blocks isa Expr
196+ blocks
197+ else
198+ Expr (:block ,
199+ blocks[1 ], # the timing setup
200+ Expr (:tryfinally ,
201+ :($ (esc (args[end ]))), # the user expr
202+ :($ (blocks[2 ])) # the timing finally
203+ )
204+ )
205+ end
184206end
185207
186208function enable_debug_timings (m:: Module )
@@ -204,26 +226,69 @@ function is_func_def(f)
204226 end
205227end
206228
229+ function _esc (ex)
230+ if isa (ex, Expr)
231+ esc (ex)
232+ else
233+ esc (ex[1 ]), esc (ex[2 ])
234+ end
235+ end
236+
207237function timer_expr (m:: Module , is_debug:: Bool , ex:: Expr )
208238 is_func_def (ex) && return timer_expr_func (m, is_debug, :($ (TimerOutputs. DEFAULT_TIMER)), ex)
209- return esc (_timer_expr (m, is_debug, :($ (TimerOutputs). DEFAULT_TIMER), ex ))
239+ return _esc (_timer_expr (m, is_debug, :($ (TimerOutputs). DEFAULT_TIMER)))
210240end
211241
212242function timer_expr (m:: Module , is_debug:: Bool , label_or_to, ex:: Expr )
213243 is_func_def (ex) && return timer_expr_func (m, is_debug, label_or_to, ex)
214- return esc (_timer_expr (m, is_debug, :($ (TimerOutputs). DEFAULT_TIMER), label_or_to, ex ))
244+ return _esc (_timer_expr (m, is_debug, :($ (TimerOutputs). DEFAULT_TIMER), label_or_to))
215245end
216246
217247function timer_expr (m:: Module , is_debug:: Bool , label:: String , ex:: Expr )
218248 is_func_def (ex) && return timer_expr_func (m, is_debug, :($ (TimerOutputs). DEFAULT_TIMER), ex, label)
219- return esc (_timer_expr (m, is_debug, :($ (TimerOutputs). DEFAULT_TIMER), label, ex ))
249+ return _esc (_timer_expr (m, is_debug, :($ (TimerOutputs). DEFAULT_TIMER), label))
220250end
221251
222252function timer_expr (m:: Module , is_debug:: Bool , to, label, ex:: Expr )
223253 is_func_def (ex) && return timer_expr_func (m, is_debug, to, ex, label)
224- return esc (_timer_expr (m, is_debug, to, label, ex))
254+ return _esc (_timer_expr (m, is_debug, to, label))
255+ end
256+
257+ # no ex given, so just return before and after for construction in the macro
258+ function _timer_expr (m:: Module , is_debug:: Bool , to:: Union{Symbol, Expr, TimerOutput} , label)
259+ @gensym local_to enabled accumulated_data b₀ t₀ val
260+ timeit_block = quote
261+ $ local_to = $ to
262+ $ enabled = $ local_to. enabled
263+ if $ enabled
264+ $ accumulated_data = $ (push!)($ local_to, $ label)
265+ end
266+ $ b₀ = $ (gc_bytes)()
267+ $ t₀ = $ (time_ns)()
268+ end
269+ finally_block = quote
270+ if $ enabled
271+ $ (do_accumulate!)($ accumulated_data, $ t₀, $ b₀)
272+ $ (pop!)($ local_to)
273+ end
274+ end
275+
276+ if is_debug
277+ return quote
278+ if $ m. timeit_debug_enabled ()
279+ $ timeit_block
280+ end
281+ end , quote
282+ if $ m. timeit_debug_enabled ()
283+ $ finally_block
284+ end
285+ end
286+ else
287+ return timeit_block, finally_block
288+ end
225289end
226290
291+ # ex given, so return the whole thing
227292function _timer_expr (m:: Module , is_debug:: Bool , to:: Union{Symbol, Expr, TimerOutput} , label, ex:: Expr )
228293 @gensym local_to enabled accumulated_data b₀ t₀ val
229294 timeit_block = quote
0 commit comments