Skip to content

Instantly share code, notes, and snippets.

@ityonemo
Created May 14, 2014 22:09
Show Gist options
  • Save ityonemo/395bab6dad4ce2555200 to your computer and use it in GitHub Desktop.
Save ityonemo/395bab6dad4ce2555200 to your computer and use it in GitHub Desktop.
#!/usr/bin/julia
#julia matrix test program.
v = rand(5)
m = rand(5,5)
r = zeros(5)
println("right product")
tic()
for idx=1:100
r = m * v
end
toc() #==> 0.25 seconds
v = v'
println("left product")
#uses memory-appropriate ordering, but performs worse.
tic()
for idx=1:100
r = v * m
end
toc() #==> 0.28 seconds
println("poorly devectorized product")
#a first, naive shot at devectorization
v = rand(5)
m = rand(25)
tic()
for idx=1:100
r = zeros(5)
for jdx = 1:5
q = jdx * 5
r += m[q - 4 : q] .* v
end
end
toc() #==> 0.50 seconds
println("devectorized product")
# reduce number of operations in the matrix multiplication loop.
# use short-cycle addition operations only.
tic()
for idx=1:100
r = zeros(5)
a = 1
b = 5
for jdx = 1:5
r += m[a : b] .* v
a += 5
b += 5
end
end
toc() #==> 0.01 seconds!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment