performance - Strange observation about timing comparison between Julia and Matlab -


the goal of experiment compare speed of matlab , julia small piece of code below.

first matlab code:

>> t = 5000; n = 10000;  x = 1:t*n; >> x = reshape(x, t, n); >> tic(); y1 = sum(x(:) .* x(:)); toc() elapsed time 0.229563 seconds. >> y1      y1 =          4.1667e+22 >> tic(); y2 = trace(x * x'); toc() elapsed time 15.332694 seconds.  >> y2 y2 =        4.1667e+22 

versus in julia

julia> t = 5000; n = 10000; x = 1: t*n;     julia> x = reshape(x, t, n); julia> tic(); y1 = sum(x[:].* x[:]); toc(); elapsed time: 1.235170533 seconds julia> y1 -4526945843202100544 julia> tic();y2 = trace(x*x'); toc(); 

the second 1 did not finish job in more 1 minutes. matter here julia? piece of code happen run both slower , overflow in julia? there problem in style? think 1 reason convert matlab julia speed, , used think julia handles big number arithmetic operations default. now, looks these not correct. can explain?

there couple of things going on here.

firstly, unlike matlab, x array of machine integers, not floating point values. appears main difference in speed, unable use blas routines linear algebra.

you need either

x = 1.0:t*n 

or explicitly convert via

x = float(x) 

this reason different answer: julia uses machine arithmetic, integers wrap around on overflow (hence negative number). won't have problem floating point values.

(julia have arbitrary-precision integers, doesn't promote default: instead need promote them via big(n))

this give speed up, can better. julia require different programming style matlab. see links isaiah provided in comments above.

in regards specific examples

sum(x[:].* x[:]) 

this slow creates 3 intermediate vectors (2 copies of x[:], though change in future, , product).

similarly,

trace(x*x') 

computes intermediate matrix (most of not used in trace).

my suggestion use

dot(vec(x),vec(x)) 

vec(x) reshapes x vector (so no copying), , dot usual sum-product. "roll-your-own":

function test(x)     s = zero(eltype(x)) # prevents type-instability     xi in x         s += xi*xi     end     s end  test(x) 

(this needs in function jit compiler work magic). should reasonably fast, though still not fast dot, uses blas calls underneath.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -