Hello Fellas,
I am relatively new to Julia Programming. So far, Julia’s high performance has caught my attention. However, it was disappointing to see that MATLAB performs 20 times faster in executing the following program. I tested this program on two different workstations, getting consistent results.
Context: This program solves Laplace’s Equation using the (Central) Finite difference method in a nested Loop fashion. The program uses elementary programming structures (no packages are imported !).
n = 100
T = zeros(n, n)
Tₖ₋₁ = zeros(n, n)
T[1, 1:n] .= 500
T[n, 1:n] .= 500
T[1:n, 1] .= 200
T[1:n, n] .= 500
error = 1.0;
tolerance = 1E-6
@time begin
while error > tolerance
Tₖ₋₁ .= T
for i = 2:n-1
for j = 2:n-1
T[i, j] = 1/4*(T[i,j+1] + T[i,j-1] + T[i+1,j] + T[i-1,j])
end
end
global error = maximum(abs.(Tₖ₋₁ .- T))
end
end
The MATLAB version is a literal translation of the Julia code and can be downloaded from (MATLAB laplacEq - Google Drive)
Results:
Julia (1.8.3) → 35.892180 seconds (901.65 M allocations: 16.409 GiB, 2.00% gc time, 0.62% compilation time)
MATLAB (R2022b) → Elapsed time is 1.578888 seconds.
There are two relevant questions arising from this problem:
- Why is Julia outperformed by MATLAB in this case?
- What can be done to improve Julia’s performance?
Since this is a fairly simple program with no dependency on additional packages, solving this puzzle might help new Julia users (like me) to develop Julia code with good programming practices, building up a strong foundation from the start.
All the best,
Santiago