I’m in the planing stages for writing a piece of scientific software and I would like to use Julia for it, but I’m surprised how much slower Julia is from Python when tested on this simple code:
function test(n::Int64,k::Int64,tmp2::Array{Float64,2})
for i in 1:k
t = rand(n, n)
for j in 1:k
tmp1 = rand(n, n)
tmp2[(i-1)*n+1:i*n,(j-1)*n+1:j*n] = t*tmp1
end
end
end;
The corresponding python code is
def test(n,k,tmp2):
for i in range(k):
t = rand(n, n)
for j in range(k):
tmp1 = rand(n, n)
tmp2[i*n:(i+1)*n,j*n:(j+1)*n] = t@tmp1
When tested with, for example,
n = 300
k = 30
tmp2=zeros((n*k,n*k))
Julia needs more than a second
@time test(n,k,tmp2)
1.122870 seconds (3.67 k allocations: 1.227 GiB, 15.44% gc time)
while python is 30% faster
%%timeit
test(n,k,tmp2)
853 ms ± 7.68 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
When testing the same function but with
tmp2[(i-1)*n+1:i*n,(j-1)*n+1:j*n] = t*tmp1
changed to some element-wise operation on the array, Julia is much faster than python, so I suppose this has to do with matrix operations.
I’m using Julia 1.4 on a Macbook pro. I tried switching from openblas to mkl but this did not improved the speed of Julia.
I would be thankful for any hint what I’m doing wrong, or Julia indeed is so slow when dealing with lots of matrix operations.