Benchmarks like this is kind of useless because you're doing a meaningless
task, that one of the systems optimize away, but the other faithfully does
in the inefficient manner you prescribed.
This is because Octave (and Matlab) uses copy on write (COW) semantics and
has spent huge efforts to use memory efficient in vectorized expressions.
That way they don't need to copy anything, and save a tremendous amount of
time on such simple benchmarks on copying performance. Copy on write is
often easier for users, but much harder to deeply understand in order to
implement efficient algorithms. It's easy to construct similar benchmarks
where the COW semantics causes similar performance issues the other way, so
that is definitely a double edged sword.
Another issue here is that Julia has to do twice as much copying as you
might expect, because a[1:n,1:n] currently creates a copy instead of a view
to the same memory, and then it needs to be copied again into b. One of the
targets for Julia 0.4 is to make a[1:n,1:n] return a View instead of a full
copy and that will make a huge difference.
The most efficient way to do what you are trying (without cheating by using
the observation that nothing really needs to be done) is to replace the
code with copy!(b,a). You will see that the execution time is cut to around
1/3, because you elimit lots of temporary arrays and half of the copying.
PS: Generally Julia also prefer to be benchmarked in a function that
doesn't access globals, so that we get full benefit of our JIT compiler.
Real performance sensitive code is usually written that way, so thats the
code we want to be really sure is fast. This doesn't matter much in this
example, but if your benchmark isn't limited by calls to library code,
you'll see a huge difference.
n=1000;
b = Array(Float64,n,n);
a=rand(n,n);
function test(n,b,a)
for i=1:10000
b[1:n,1:n]=a[1:n,1:n];
#b=a;
end
end
@time test(n,b,a)
@time test(n,b,a)
torsdag 5. mars 2015 09.15.34 UTC+1 skrev Ranjan Anantharaman følgende:
>
> Hello I was comparing the performance of two snippets of MATLAB (running
> on Octave) and Julia code on Windows.
>
> MATLAB code:
>
> n=1000;
> a=rand(n,n);
>
> tic
> for i=1:10000
> b(1:n,1:n)=a(1:n,1:n);
> %b=a;
> end
> toc
>
> JULIA Code:
>
> n=1000;
> b = Array(Float64,n,n);
> a=rand(n,n);
>
> tic();
> for i=1:10000
> b[1:n,1:n]=a[1:n,1:n];
> #b=a;
> end
> toc();
>
> I understand that both the statements in the loop are b=a, but I decided
> to index the arrays and see what happened.
>
> The MATLAB code takes 0.22 seconds (on Octave ) whereas the JULIA code
> takes between 80 to 100 seconds (varies every time).
>
> I think Octave is performing some simple optimization here, and I think
> Julia might benefit from the same.
>
> Version info: Octave 3.6.4 and Julia 0.4.0-dev (but I saw the same happen
> in 0.3.5 too)
>
>
>