Octave seems to be doing a pretty good job for this type of calculation. If
you want to squeeze a bit more performance out of Julia, you can try to use
explicit loops (devectorize as in
http://julialang.org/blog/2013/09/fast-numeric/). Then you might also
remove bounds checking in a loop for faster performance.
Try this function:
function doCalc(x::Array{Float64,2})
XX=Array(Float64, 7000,7000)
for j=1:7000,i=1:7000
@inbounds XX[i,j]=x[i,j]*x[i,j]
end
XX
end
Followed by
@time XX=doCalc(X);
On Monday, September 8, 2014 11:36:02 AM UTC+3, Ján Dolinský wrote:
>
> Hello,
>
> I am a new Julia user. I am trying to write a function for computing
> "self" dot product of all columns in a matrix, i.e. calculating a square of
> each element of a matrix and computing a column-wise sum. I am interested
> in a proper way of doing it because I often need to process large matrices.
>
> I first put a focus on calculating the squares. For testing purposes I use
> a matrix of random floats of size 7000x7000. All timings here are deducted
> after several repetitive runs.
>
> I used to do it in Octave (v3.8.1) a follows:
> tic; X = rand(7000); toc;
> Elapsed time is 0.579093 seconds.
> tic; XX = X.^2; toc;
> Elapsed time is 0.114737 seconds.
>
>
> I tried to to the same in Julia (v0.2.1):
> @time X = rand(7000,7000);
> elapsed time: 0.114418731 seconds (392000128 bytes allocated)
> @time XX = X.^2;
> elapsed time: 0.369641268 seconds (392000224 bytes allocated)
>
> I was surprised to see that Julia is about 3 times slower when calculating
> a square than my original routine in Octave. I then read "Performance tips"
> and found out that one should use * instead of of raising to small integer
> powers, for example x*x*x instead of x^3. I therefore tested the
> following.
> @time XX = X.*X;
> elapsed time: 0.146059577 seconds (392000968 bytes allocated)
>
> This approach indeed resulted in a lot shorter computing time. It is still
> however a little slower than my code in Octave. Can someone advise on any
> performance tips ?
>
> I then finally do a sum over all columns of XX to get the "self" dot
> product but first I'd like to fix the squaring part.
>
> Thanks a lot.
> Best Regards,
> Jan
>
> p.s. In Julia manual I found a while ago an example of using @vectorize
> macro with a squaring function but can not find it any more. Perhaps the
> name of macro was different ...
>
>