I recently added (and tagged) functionality to annotate function definitions. This is sometimes more convenient than adding them at the call site or surrounding a whole function block with the macro. Annotating a function definition is done with the same @timeit
macro and this will record a section with the same name as the function when the function is called.
For example:
julia> using TimerOutputs
julia> const to = TimerOutput();
julia> @timeit to f(x) = x
f (generic function with 1 method)
julia> @timeit to function g(x)
# Doing stuff
return sin(x)
end
g (generic function with 1 method)
julia> for i in 1:100 f(2) end;
julia> for j in 1:10 g(2.0) end;
julia> to
──────────────────────────────────────────────────────────────────
Time Allocations
────────────────────── ───────────────────────
Tot / % measured: 9.39s / 0.00% 1.08MiB / 0.00%
Section ncalls time %tot avg alloc %tot avg
──────────────────────────────────────────────────────────────────
f 100 5.15μs 75.2% 51.5ns - - % -
g 10 1.70μs 24.8% 170ns - - % -
──────────────────────────────────────────────────────────────────
The timer to
can be left out and the global one will be used in that case:
julia> @timeit h(x) = x
h (generic function with 1 method)
julia> h(2)
2
julia> print_timer()
──────────────────────────────────────────────────────────────────
Time Allocations
────────────────────── ───────────────────────
Tot / % measured: 3.88s / 0.00% 289KiB / 0.00%
Section ncalls time %tot avg alloc %tot avg
──────────────────────────────────────────────────────────────────
h 1 295ns 100% 295ns - - % -
──────────────────────────────────────────────────────────────────
Happy timing!