4

Just now, I came to know that Matlab & Octave uses column-major from Wikipedia.

Column-major order is used in Fortran, OpenGL and OpenGL ES, MATLAB, GNU Octave, R, Julia, Rasdaman, and Scilab.

So I was just checking the "for loop" speed in both Matlab and Octave. Below are the results:

Matlab

>> x = rand(10000);
>> tic; for i=1:10000 for j=1:10000 k=x(i,j); end; end; toc; % row-major
Elapsed time is 2.320215 seconds.
>> tic; for i=1:10000 for j=1:10000 k=x(j,i); end; end; toc; % column-major
Elapsed time is 0.433013 seconds.

As expected, Column-major order is faster than row-major order.

Octave

> x=rand(5000);
> tic; for i=1:5000 for j=1:5000 k=x(i,j); end; end; toc; % row-major
Elapsed time is 77 seconds.
> tic; for i=1:5000 for j=1:5000 k=x(j,i); end; end; toc; % column-major
Elapsed time is 78 seconds.

The results are same in both the cases.

Question: Why both row-major and column-major looping perform similar in Octave, although it uses column-major alignment?

Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
  • 2
    This might explain something, particularly Octave's lack of JIT: http://stackoverflow.com/questions/12569351/why-is-octave-slower-than-matlab – David Mar 28 '14 at 03:57
  • 1
    I have seen that link already. It gives an insight to why Matlab loops are much much faster compared to octave (0.5 sec for 10000x10000 v/s 78 sec for 5000x5000 in Octave), but not mentioning anything about my question, I think. – Abid Rahman K Mar 28 '14 at 04:02
  • 2
    Running your tests again with `feature accel off` I see no real differences between row or column first versions (very similar to your Octave results). So the JIT compilation is the difference in times, and I guess the non-vectorised loops don't rely on how the matrix is accessed as much as the vectorised version that the JIT compiler is using. – David Mar 28 '14 at 04:12
  • 1
    Oh, that was cool. But again, why no difference in both Matlab and Octave? I mean, any way, column-wise looping should be faster, right? – Abid Rahman K Mar 28 '14 at 04:40

2 Answers2

3

To expand on my comments, I will post an answer. I am no expert on this though.

Firstly, running the tests in Matlab with feature accel off to disable JIT compilation returns results roughly the same as Octave's. Thus the speed increase between Matlab and Octave is due to Matlab's JIT Compilation, which must be vectorising the loop.

Secondly, the reason that Matlab's JIT compiled version is faster for column-first rather than row-first is because Matlab is column-major, this makes sense.

Then for both Octave and Matlab without JIT compilation, the column-first evaluation is just a tiny bit quicker. Since each value is pulled from the random matrix individually, I can see why this would be slow, but not why there should be much difference between row or column first.

Hope that helps anyway, and maybe someone else can expand/improve this or post a real answer!

David
  • 8,449
  • 1
  • 22
  • 32
  • +1 for the answer. But I still didn't get why there is no difference without JIT. – Abid Rahman K Mar 28 '14 at 08:25
  • I think it's just due to the fact that each loop iteration, Matlab/Octave has to go and retrieve a single element of the matrix, do the calculation with it, then retrieve the next element. It isn't loading columns of the matrix into cache and using each element, it is loading single elements of the cache, then removing (?) them, then loading the next element into the cache. – David Mar 28 '14 at 22:05
2

If you try operating on entire columns or rows, you will see a difference. Try the following code in both MATLAB and Octave:

%% Columns
a = rand(n);
b = zeros(n,1);
tic
for ii = 1:n
  b = b + a(:,ii);
end
t1 = toc

%% Rows:
a = rand(n);
b = zeros(1,n);
tic
for ii = 1:n
  b = b + a(ii,:);
end
t2 = toc

On my computer this gives:

%% MATLAB:
t1 =  0.0287
t2 =  0.3778

%% Octave*:
t1 =  0.089480
t2 =  0.20313

Now, the difference is not as big in Octave as in MATLAB, but it's still a significant difference in Octave (more than twice as fast).

David has already posted an answer talking about the JIT compilation, so I'll skip that part here.

*I don't have Octave available, so I had to test it here.

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70