Created
September 29, 2015 09:04
-
-
Save tomasaschan/f0dc12b7c211f73f6cc1 to your computer and use it in GitHub Desktop.
Globals vs Const Globals vs Closures
This file contains hidden or 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 | |
| g(f::Function, 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 | |
| println("warmup...") | |
| @time 1 | |
| f_glob(1) | |
| f_const_glob(1) | |
| f_clos(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) | |
| nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment