-
-
Save shashi/b7a147f630d744c2b04f to your computer and use it in GitHub Desktop.
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
macro anon(ex) | |
esc(Expr(:(=), Expr(:call, gensym(), ex.args[1]),ex.args[2])) | |
end | |
function testn(f, n) | |
s = 0.0 | |
for i = 1:n | |
s += f(i/n) | |
end | |
s | |
end | |
function test_inlined(n) | |
s = 0.0 | |
for i = 1:n | |
s += (i/n+1.2)^2 | |
end | |
s | |
end | |
println("anonymous function") | |
offset = 1.2 | |
@time testn(x->(x+offset)^2, 10^7) | |
println("named function") | |
g(x) = (x+offset)^2 | |
@time testn(g, 10^7) | |
println("`function` function") | |
function h(x) | |
(x+offset)^2 | |
end | |
@time testn(h, 10^7) | |
println("type") | |
type t | |
x | |
end | |
t(x) = (x+offset)^2 | |
@time testn(t, 10^7) | |
println("immutable") | |
type I | |
x | |
end | |
I(x) = (x+offset)^2 | |
@time testn(I, 10^7) | |
println("inlined") | |
@time test_inlined(10^7) | |
println("Second pass") | |
... | |
######## Output ######### | |
anonymous function | |
elapsed time: 2.645531369 seconds (640112020 bytes allocated, 13.55% gc time) | |
named function | |
elapsed time: 3.175482231 seconds (800010776 bytes allocated, 14.05% gc time) | |
psuedonymous(?) function | |
elapsed time: 3.268614429 seconds (800011112 bytes allocated, 14.76% gc time) | |
`function` function | |
elapsed time: 3.177589198 seconds (800010776 bytes allocated, 14.16% gc time) | |
type | |
elapsed time: 3.204535867 seconds (800089032 bytes allocated, 14.88% gc time) | |
immutable | |
elapsed time: 3.148536633 seconds (800010776 bytes allocated, 14.22% gc time) | |
inlined | |
elapsed time: 0.117864924 seconds (93944 bytes allocated) | |
Second pass | |
anonymous function | |
elapsed time: 2.597348314 seconds (640001328 bytes allocated, 14.18% gc time) | |
named function | |
elapsed time: 3.18961047 seconds (800010600 bytes allocated, 14.01% gc time) | |
psuedonymous(?) function | |
elapsed time: 3.214356453 seconds (800011112 bytes allocated, 15.13% gc time) | |
`function` function | |
elapsed time: 3.195201891 seconds (800010600 bytes allocated, 14.11% gc time) | |
type | |
elapsed time: 3.230227238 seconds (800010600 bytes allocated, 14.70% gc time) | |
immutable | |
elapsed time: 3.202944042 seconds (800010600 bytes allocated, 14.15% gc time) | |
inlined | |
elapsed time: 0.113643034 seconds (112 bytes allocated) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment