Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bechmark of slicedim for BItArray
using BenchmarkTools
function dobench(n, nd)
x = falses(ntuple(x -> n, nd)...)
@benchmark slicedim($x, 1, 3)
end
function dobench()
println(dobench(5, 1))
println(dobench(10000, 1))
println(dobench(100, 3))
println(dobench(10, 8))
end
println("Without specialization")
dobench()
function Base.slicedim(A::BitArray, d::Integer, i::Integer)
d_in = size(A)
leading = d_in[1:(d-1)]
d_out = tuple(leading..., d_in[(d+1):end]...)
M = prod(leading)
N = length(A)
stride = M * d_in[d]
B = BitArray(d_out)
index_offset = 1 + (i-1)*M
l = 1
if M == 1
for j = 0:stride:(N-stride)
B[l] = A[j + index_offset]
l += 1
end
else
for j = 0:stride:(N-stride)
offs = j + index_offset
for k = 0:(M-1)
B[l] = A[offs + k]
l += 1
end
end
end
return B
end
println("With specialization")
dobench()
Without specialization
BenchmarkTools.Trial:
samples: 10000
evals/sample: 996
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 16.00 bytes
allocs estimate: 1
minimum time: 24.00 ns (0.00% GC)
median time: 24.00 ns (0.00% GC)
mean time: 24.77 ns (1.45% GC)
maximum time: 1.01 μs (96.53% GC)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 996
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 16.00 bytes
allocs estimate: 1
minimum time: 22.00 ns (0.00% GC)
median time: 23.00 ns (0.00% GC)
mean time: 24.34 ns (1.48% GC)
maximum time: 989.00 ns (96.36% GC)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 1.58 kb
allocs estimate: 12
minimum time: 25.21 μs (0.00% GC)
median time: 25.49 μs (0.00% GC)
mean time: 25.83 μs (0.00% GC)
maximum time: 65.44 μs (0.00% GC)
BenchmarkTools.Trial:
samples: 208
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 1.19 mb
allocs estimate: 58
minimum time: 24.00 ms (0.00% GC)
median time: 24.04 ms (0.00% GC)
mean time: 24.11 ms (0.06% GC)
maximum time: 25.18 ms (2.68% GC)
With specialization
BenchmarkTools.Trial:
samples: 10000
evals/sample: 6
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 352.00 bytes
allocs estimate: 6
minimum time: 5.30 μs (0.00% GC)
median time: 5.44 μs (0.00% GC)
mean time: 5.48 μs (0.00% GC)
maximum time: 13.01 μs (0.00% GC)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 6
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 416.00 bytes
allocs estimate: 10
minimum time: 5.29 μs (0.00% GC)
median time: 5.44 μs (0.00% GC)
mean time: 5.52 μs (0.67% GC)
maximum time: 386.14 μs (96.07% GC)
BenchmarkTools.Trial:
samples: 2378
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 930.95 kb
allocs estimate: 49482
minimum time: 2.00 ms (0.00% GC)
median time: 2.06 ms (0.00% GC)
mean time: 2.10 ms (1.96% GC)
maximum time: 3.55 ms (41.02% GC)
BenchmarkTools.Trial:
samples: 2
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 1.19 gb
allocs estimate: 69999346
minimum time: 2.95 s (1.85% GC)
median time: 2.95 s (1.87% GC)
mean time: 2.95 s (1.87% GC)
maximum time: 2.95 s (1.88% GC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment