Created
September 29, 2015 09:48
-
-
Save afniedermayer/58ca3ba2c418617d85b4 to your computer and use it in GitHub Desktop.
Globals vs Const Globals vs Closures vs Call Overloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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