eig()/eigfact() performance: Julia vs. MATLAB

577 views
Skip to first unread message

Evgeni Bezus

unread,
Jul 12, 2015, 2:33:56 PM7/12/15
to julia...@googlegroups.com
Hi all,

I am a Julia novice and I am considering it as a potential alternative to MATLAB.
My field is computational nanophotonics and the main numerical technique that I use involves multiple solution of the eigenvalue/eigenvector problem for dense matrices with size of about 1000*1000 (more or less).
I tried to run the following nearly equivalent code in Julia and in MATLAB:

Julia code:

n = 1000
M = rand(n, n)
F = eigfact(M)
tic()
for i = 1:10
    F = eigfact(M)
end
toc()


MATLAB code:

n = 1000;
M = rand(n, n);
[D, V] = eig(M);
tic;
for i = 1:10
    [D, V] = eig(M);
end
toc

It turns out that MATLAB's eig() runs nearly 2.3 times faster than eig() or eigfact() in Julia. On the machine available to me right now (relatively old Core i5 laptop) the average time for MATLAB is of about 37 seconds, while the mean Julia time is of about 85 seconds. I use MATLAB R2010b and Julia 0.3.7 (i tried to run the code both in Juno and in a REPL session and obtained nearly identical results).

Is there anything that I'm doing wrong?

Best regards,
Evgeni

John Myles White

unread,
Jul 12, 2015, 2:38:13 PM7/12/15
to julia...@googlegroups.com, evgeni...@gmail.com

Milan Bouchet-Valat

unread,
Jul 12, 2015, 2:57:40 PM7/12/15
to julia...@googlegroups.com, evgeni...@gmail.com
Le dimanche 12 juillet 2015 à 11:38 -0700, John Myles White a écrit :
> http://julia.readthedocs.org/en/release-0.3/manual/performance-tips/
I don't think running the code in the global scope is the problem here:
most of the computing time is probably in BLAS anyway. I think MATLAB
uses Intel MKL while Julia uses OpenBLAS, and maybe on that particular
problem and with your particular machine the former is significantly
faster.

If you really need this 2.3 factor you could try building Julia with
MKL. See https://github.com/JuliaLang/julia/#intel-compilers-and-math
-kernel-library-mkl


Regards

Mauricio Esteban Cuak

unread,
Jul 12, 2015, 6:41:26 PM7/12/15
to julia...@googlegroups.com
I tried in in Matlab R2014a and Julia 0.3.10 on an 2.5 GHZ i5 and the difference was much smaller:

22 seconds for Julia, 19 for Matlab

Also, I tried it in local and global scope and the difference wasn't more than one or two seconds

Sheehan Olver

unread,
Jul 12, 2015, 8:19:33 PM7/12/15
to julia...@googlegroups.com
I remember seeing this same performance gap before.  I believe the problem is that OpenBLAS doesn't have the correct thread defaults. Here are other timings setting the threads directly:

julia> blas_set_num_threads(1)


julia> @time eigfact(M);

elapsed time: 1.827510669 seconds (79997048 bytes allocated, 1.88% gc time)


julia> blas_set_num_threads(2)


julia> @time eigfact(M);

elapsed time: 1.549618631 seconds (79997048 bytes allocated)


julia> blas_set_num_threads(3);@time eigfact(M);

elapsed time: 1.498852226 seconds (79997048 bytes allocated, 2.63% gc time)


julia> blas_set_num_threads(4);@time eigfact(M);

elapsed time: 2.062847561 seconds (79997048 bytes allocated)

Sheehan Olver

unread,
Jul 12, 2015, 8:20:41 PM7/12/15
to julia...@googlegroups.com
Sorry, forgot the timing with the default number of threads.

julia> @time eigfact(M);

elapsed time: 2.261110895 seconds (79997048 bytes allocated, 2.05% gc time)

Evgeni Bezus

unread,
Aug 4, 2015, 10:16:25 AM8/4/15
to julia-users
Thank you all for your replies.

I have tried to run the following modified Julia code at my work PC (Windows 7, Core i7-2600 , 16 GB RAM):

function f(M)
  for i = 1:10
    F = eigfact(M)
  end
end

blas_set_num_threads(1)
n = 1000
M = rand(n, n)
F = eigfact(M)
@time f(M)

The MATLAB code remained the same:

n = 1000;
M = rand(n, n);
[D, V] = eig(M);
tic;
for i = 1:10
    [D, V] = eig(M);
end
toc

It turned out that in this case Julia time (~17 sec.) exceeds the MATLAB time (~14.5 sec) by less than 20% which is quite acceptable for me. Moreover, the laptop time values are almost the same.



понедельник, 13 июля 2015 г., 4:20:41 UTC+4 пользователь Sheehan Olver написал:
Reply all
Reply to author
Forward
0 new messages