Skip to content

Instantly share code, notes, and snippets.

@afniedermayer
Created September 29, 2015 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save afniedermayer/58ca3ba2c418617d85b4 to your computer and use it in GitHub Desktop.
Save afniedermayer/58ca3ba2c418617d85b4 to your computer and use it in GitHub Desktop.
Globals vs Const Globals vs Closures vs Call Overloading
a = 3
const b = 3
immutable Parameters
val::Int64
end
g(f, N) = f(N)
function f_glob(N)
s = 0
for i = 1:N
s += a
end
s
end
function f_const_glob(N)
s = 0
for i = 1:N
s += b
end
s
end
function f_clos(N, c)
function f_inner(N)
s = 0
for i = 1:N
s += c
end
s
end
g(f_inner, N)
end
function call(p::Parameters, N)
s = 0
for i = 1:N
s += p.val
end
s
end
function f_call(N, c)
g(Parameters(c), N)
end
println("warmup...")
@time 1
f_glob(1)
f_const_glob(1)
f_clos(1, 3)
f_call(1, 3)
println("Non-const global")
gc(); @time f_glob(10^6)
println("Const global")
gc(); @time f_const_glob(10^6)
println("Closure")
gc(); @time f_clos(10^6, 3)
println("Call Overloading")
gc(); @time f_call(10^6, 3)
nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment