Skip to content

Instantly share code, notes, and snippets.

@ScottPJones
Created June 8, 2016 13:23
Show Gist options
  • Save ScottPJones/d2b6fa941312732d759442749e39b598 to your computer and use it in GitHub Desktop.
Save ScottPJones/d2b6fa941312732d759442749e39b598 to your computer and use it in GitHub Desktop.
Updated benchmark of Chris Raukaukas Ellipsis benchmark, from blog
function c1()
u = Array{Int}(4,4,3)
u[:,:,1] = [1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1]
u[:,:,2] = [1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1]
u[:,:,3] = [1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1]
u
end
function t1(u,n)
j = 1
for i = 1:n
j += sum(u[:,:,1] + u[:,:,2] + 3u[:,:,3] + u[:,:,i%3+1] - u[:,:,(i%j)%2+1])
end
j
end
function c2()
u = Vector{Matrix{Int}}(3)
u[1] = [1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1]
u[2] = [1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1]
u[3] = [1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1]
u
end
function t2(u,n)
j = 1
for i = 1:n
j += sum(u[1] + u[2] + 3u[3] + u[i%3+1] - u[(i%j)%2+1])
end
j
end
function c3()
u = Array{Int}(4,4,4)
u[1,:,:] = reshape([1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1],(1,4,4))
u[2,:,:] = reshape([1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1],(1,4,4))
u[3,:,:] = reshape([1 2 3 4
1 3 3 4
1 5 6 3
5 2 3 1],(1,4,4))
u
end
function t3(u,n)
j = 1
for i = 1:n
j += sum(u[1,:,:] + u[2,:,:] + 3u[3,:,:] + u[i%3+1,:,:] - u[(i%j)%2+1,:,:])
end
j
end
# Create arrays
const u1 = c1()
const u2 = c2()
const u3 = c3()
const N = 100000
test1() = for i=1:10 ; t1(u1,N) ; end
test2() = for i=1:10 ; t2(u2,N) ; end
test3() = for i=1:10 ; t3(u3,N) ; end
#Pre-compile
test1()
test2()
test3()
@time test1()
@time test2()
@time test3()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment