Skip to content

Instantly share code, notes, and snippets.

@yomichi
Last active August 29, 2015 14:10
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 yomichi/916c05add8796a836841 to your computer and use it in GitHub Desktop.
Save yomichi/916c05add8796a836841 to your computer and use it in GitHub Desktop.
sample and result of @inline macro
#=
These sample functions are taken from https://github.com/JuliaLang/julia/pull/8297
=#
function f(x)
y = x+5
z = y*y
q = z/y
m = q-3
end
@inline function f_inlined(x)
y = x+5
z = y*y
q = z/y
m = q-3
end
g(x) = f(2x)
g_inlined(x) = f_inlined(2x)
gc_disable()
# JIT compile
x = 0
f(x)
f_inlined(x)
g(x)
g_inlined(x)
N = 1 << 20
@show N
println()
println("f(x)")
@time for i in 1:N
x = f(x)
end
println()
x = 0
println("f_inlined(x)")
@time for i in 1:N
x = f_inlined(x)
end
println()
println("g(x) = f(2x)")
@time for i in 1:N
x = g(x)
end
println()
x = 0
println("g_inlined(x) = f_inlined(2x)")
@time for i in 1:N
x = g_inlined(x)
end
println()
x = 0
println("not function")
@time for i in 1:N
y = x+5
z = y*y
q = z/y
m = q-3
end
$ julia -E 'versioninfo()'
Julia Version 0.4.0-dev+1903
Commit 6b0fcce* (2014-11-30 18:08 UTC)
Platform Info:
System: Darwin (x86_64-apple-darwin13.4.0)
CPU: Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK: libopenblas
LIBM: libopenlibm
LLVM: libLLVM-3.3
$ julia inline.jl
N = 1048576
f(x)
elapsed time: 0.181024115 seconds (83911928 bytes allocated)
f_inlined(x)
elapsed time: 0.45793796 seconds (134205080 bytes allocated)
g(x) = f(2x)
elapsed time: 0.405215525 seconds (100649072 bytes allocated)
g_inlined(x) = f_inlined(2x)
elapsed time: 0.181524273 seconds (83937752 bytes allocated)
not function
elapsed time: 0.277514884 seconds (100646984 bytes allocated)
(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment