Skip to content

Instantly share code, notes, and snippets.

@mortenpi
Created October 3, 2015 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mortenpi/d4e5d8a3b173a02f3339 to your computer and use it in GitHub Desktop.
Save mortenpi/d4e5d8a3b173a02f3339 to your computer and use it in GitHub Desktop.
Time a function multiple times to see how memory usage behaves.
import Base: gc_bytes, gc_time_ns, time_print
function lm_1_10(x1, x10)
A = (x10-x1)/9
B = (10*x1-x10)/9
A,B
end
macro time_N(N, ex)
quote
local b0 = gc_bytes()
local t0 = time_ns()
local g0 = gc_time_ns()
for i=1:$(esc(N))
$(esc(ex))
end
local g1 = gc_time_ns()
local t1 = time_ns()
local b1 = gc_bytes()
t1-t0, b1-b0, g1-g0
end
end
macro time_lm(ex)
quote
local val = $(esc(ex))
dt1, db1, dg1 = @time_N 1 $(esc(ex))
dt10, db10, dg10 = @time_N 10 $(esc(ex))
tA,tB = lm_1_10(dt1, dt10)
bA,bB = lm_1_10(db1, db10)
@printf "resources: %.2e*N + %.2e sec (%d * N + %d bytes)\n" tA/1e9 tB/1e9 bA bB
val
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment