3 ms·
I've been using Julia a lot lately so these results really surprised me, and I wrote my own test. Punchline: it takes about 0.4 seconds on Julia 0.4.6, on my ma
by Tarrosion 10y ago
I've been using Julia a lot lately so these results really surprised me, and I wrote my own test. Punchline: it takes about 0.4 seconds on Julia 0.4.6, on my machine which is several years old. Check it out:
julia> include("elmult.jl")
eachindex
0.42587028
outer loop over rows
8.923323015
outer loop over cols
0.516270241
Code:
function elmult(m1, m2)
result = zeros(m1)
for i in eachindex(m1)
result[i] = m1[i] * m2[i]
end
return result
end
function elmult_rowmajor(m1, m2)
result = zeros(m1)
nr = size(m1, 1)
nc = size(m1, 2)
for i in 1:nr, j in 1:nc
result[i,j] = m1[i,j] * m2[i,j]
end
return result
end
function elmult_colmajor(m1, m2)
result = zeros(m1)
nr = size(m1, 1)
nc = size(m1, 2)
for j in 1:nc, i in 1:nr
result[i,j] = m1[i,j] * m2[i,j]
end
return result
end
elmult(rand(3,3), rand(3,3))
elmult_colmajor(rand(3,3), rand(3,3))
elmult_rowmajor(rand(3,3), rand(3,3))
m1, m2 = rand(10000,10000), rand(10000, 10000);
println("eachindex")
println(@elapsed elmult(m1, m2))
println("outer loop over rows")
println(@elapsed elmult_rowmajor(m1, m2))
println("outer loop over cols")
println(@elapsed elmult_colmajor(m1, m2))