Skip to content

Instantly share code, notes, and snippets.

@martinholters
Last active April 13, 2016 12:20
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 martinholters/79e8bc8ea2faef66055775def4a4c58b to your computer and use it in GitHub Desktop.
Save martinholters/79e8bc8ea2faef66055775def4a4c58b to your computer and use it in GitHub Desktop.
Benchmark code for APL-style indexing Compat code
using Compat
function idxbench(N::Integer, idx...)
a=rand(repmat([3], length(idx))...)
idxbench(a, N, idx...)
end
function idxbench(a::AbstractArray, N::Integer, idx...)
b=Compat.ArrayAPL(a)
r = 0
t0 = time_ns()
for i=1:N; r += length(a[idx...]); end
t1 = time_ns()
ref_time = t1-t0
print(" | ", ref_time / N)
t0 = time_ns()
for i=1:N; r += length(b[idx...]); end
t1 = time_ns()
print(" | ", (t1-t0) / ref_time)
t0 = time_ns()
for i=1:N; r += length(@compat a[idx...]); end
t1 = time_ns()
println(" | ", (t1-t0) / ref_time, " |")
@assert r > 0
end
for init in [true, false]
# init==true is for precompilation
versioninfo()
println("| #dims | #trailing ints | time plain array | rel. time `ArrayAPL` | rel.time `@compat` |")
println("| --- | --- | --- | --- | --- |")
for n in 1:10, trail in 0:n
print("| ", n, " | ", trail)
idx = [repmat(UnitRange[1:3], n-trail); repmat([1], trail)]
# heuristic to have approximately equal runtime for every configuration
N = VERSION >= v"0.4" ? 1e8 : 1e9
N = init ? 1 : n==trail ? ceil(Int, N/n) : ceil(Int, N/1.8^n/n)
idxbench(N, idx...)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment