Skip to content

Instantly share code, notes, and snippets.

@tkf
Last active January 11, 2018 10:54
Show Gist options
  • Save tkf/7fc8a13cd077a802c8508a9b13209925 to your computer and use it in GitHub Desktop.
Save tkf/7fc8a13cd077a802c8508a9b13209925 to your computer and use it in GitHub Desktop.
using BenchmarkTools
@inline function partial_sum_inline(x, idx)
s = zero(eltype(x))
y = view(x, idx)
for i in 1:endof(y)
@inbounds s += y[i]
end
return s
end
@noinline function partial_sum_noinline(x, idx)
s = zero(eltype(x))
y = view(x, idx)
for i in 1:endof(y)
@inbounds s += y[i]
end
return s
end
function chunked_sum(partial_sum, x, chunk)
n = length(x) ÷ chunk
s = zero(eltype(x))
for i in 0:n-1
s += partial_sum(x, i*chunk+1:(i+1)*chunk)
end
return s
end
srand(1)
x = rand([-1, 1], 1000000)
@assert chunked_sum(partial_sum_inline, x, 10) == sum(x)
@assert chunked_sum(partial_sum_noinline, x, 10) == sum(x)
versioninfo()
@show Base.JLOptions().check_bounds # 0: unspecified; 1: yes; 2: no
println()
println("** inline **")
display(@benchmark chunked_sum(partial_sum_inline, x, 10))
println()
println()
println("** noinline **")
display(@benchmark chunked_sum(partial_sum_noinline, x, 10))
println()
Julia Version 0.6.2
Commit d386e40 (2017-12-13 18:08 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM)2 Quad CPU Q9400 @ 2.66GHz
WORD_SIZE: 64
BLAS: libopenblas (DYNAMIC_ARCH NO_AFFINITY Penryn)
LAPACK: libopenblas
LIBM: libm
LLVM: libLLVM-3.9.1 (ORCJIT, penryn)
Base.JLOptions().check_bounds = 0
** inline **
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 2.224 ms (0.00% GC)
median time: 2.876 ms (0.00% GC)
mean time: 2.672 ms (0.00% GC)
maximum time: 2.921 ms (0.00% GC)
--------------
samples: 1859
evals/sample: 1
** noinline **
BenchmarkTools.Trial:
memory estimate: 4.58 MiB
allocs estimate: 100001
--------------
minimum time: 4.002 ms (0.00% GC)
median time: 5.235 ms (0.00% GC)
mean time: 5.485 ms (4.50% GC)
maximum time: 8.833 ms (13.41% GC)
--------------
samples: 909
evals/sample: 1
Julia Version 0.7.0-DEV.3354
Commit 9b5eed2b6c (2018-01-09 08:03 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM)2 Quad CPU Q9400 @ 2.66GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Penryn)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.9.1 (ORCJIT, penryn)
Environment:
(Base.JLOptions()).check_bounds = 0
** inline **
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 2.040 ms (0.00% GC)
median time: 2.647 ms (0.00% GC)
mean time: 2.639 ms (0.00% GC)
maximum time: 2.701 ms (0.00% GC)
--------------
samples: 1881
evals/sample: 1
** noinline **
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 2.072 ms (0.00% GC)
median time: 2.687 ms (0.00% GC)
mean time: 2.664 ms (0.00% GC)
maximum time: 2.747 ms (0.00% GC)
--------------
samples: 1864
evals/sample: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment