Closed kongdd closed 1 year ago
Interesting. If you look at what a macro gets to work with:
julia> macro dumpit(ex)
dump(ex)
return esc(ex)
end
@dumpit (macro with 1 method)
julia> @dumpit function f(x)
x = 3; sin(x)
y = 2; sin(2)
end
Expr
head: Symbol function
args: Array{Any}((2,))
1: Expr
head: Symbol call
args: Array{Any}((2,))
1: Symbol f
2: Symbol x
2: Expr
head: Symbol block
args: Array{Any}((9,))
1: LineNumberNode
line: Int64 1
file: Symbol REPL[14]
2: LineNumberNode
line: Int64 2
file: Symbol REPL[14]
3: Expr
head: Symbol =
args: Array{Any}((2,))
1: Symbol x
2: Int64 3
4: LineNumberNode
line: Int64 2
file: Symbol REPL[14]
5: Expr
head: Symbol call
args: Array{Any}((2,))
1: Symbol sin
2: Symbol x
6: LineNumberNode
line: Int64 3
file: Symbol REPL[14]
7: Expr
head: Symbol =
args: Array{Any}((2,))
1: Symbol y
2: Int64 2
8: LineNumberNode
line: Int64 3
file: Symbol REPL[14]
9: Expr
head: Symbol call
args: Array{Any}((2,))
1: Symbol sin
2: Int64 2
f (generic function with 1 method)
you can see there are LineNumberNode
s in there. You would probably loop through each statement and for each new LineNumberNode you would insert a @timeit
call.
Thanks for your quick response and useful information
Sorry. I still has no idea how to implement this function.
This is my solution: https://github.com/jl-spatial/Ipaper.jl/blob/master/src/timeit_all.jl
julia> @timeit_all function foo(n)
for i = 1:n
begin
A = randn(100, 100, 20)
m = maximum(A)
end
if i < 10
Am = mapslices(sum, A; dims=2)
B = A[:, :, 5]
Bsort = mapslices(B; dims=1) do col
sort(col)
end
elseif i < 15
sleep(0.01)
else
sleep(0.02)
end
let j
j = i
while j < 5
b = rand(100)
C = B .* b
j += 1
end
end
end
sleep(0.5)
end
foo (generic function with 1 method)
julia> foo(20)
julia> show(to, sortby = :firstexec)
───────────────────────────────────────────────────────────────────────────────────────────────────
Time Allocations
─────────────────────── ────────────────────────
Tot / % measured: 11.8s / 13.8% 272MiB / 43.4%
Section ncalls time %tot avg alloc %tot avg
───────────────────────────────────────────────────────────────────────────────────────────────────
L5: for 1 1.12s 69.0% 1.12s 118MiB 100.0% 118MiB
L6: block 20 149ms 9.1% 7.43ms 30.5MiB 25.8% 1.53MiB
L7: A = randn(100, 100, 20) 20 140ms 8.6% 6.99ms 30.5MiB 25.8% 1.53MiB
L8: m = maximum(A) 20 2.22ms 0.1% 111μs 640B 0.0% 32.0B
L10: if 20 974ms 59.8% 48.7ms 86.9MiB 73.5% 4.34MiB
L11: Am = mapslices(sum, A; di ... 9 435ms 26.7% 48.3ms 61.8MiB 52.3% 6.87MiB
L12: B = A[:, :, 5] 9 431μs 0.0% 47.8μs 704KiB 0.6% 78.2KiB
L13: Bsort = mapslices(B; dims ... 9 278ms 17.1% 30.9ms 24.3MiB 20.6% 2.70MiB
L21: let 20 506μs 0.0% 25.3μs 797KiB 0.7% 39.8KiB
L22: j = i 20 18.8μs 0.0% 940ns 640B 0.0% 32.0B
L23: while 20 411μs 0.0% 20.5μs 794KiB 0.7% 39.7KiB
L24: b = rand(100) 10 10.9μs 0.0% 1.09μs 9.06KiB 0.0% 928B
L25: C = B .* b 10 335μs 0.0% 33.5μs 782KiB 0.6% 78.2KiB
L26: j += 1 10 6.70μs 0.0% 670ns 320B 0.0% 32.0B
L30: sleep(0.5) 1 505ms 31.0% 505ms 176B 0.0% 176B
───────────────────────────────────────────────────────────────────────────────────────────────────
Any idea how to implement the macro reported by @ffevotte https://discourse.julialang.org/t/tooling-for-line-by-line-memory-allocation-analysis/74552/6