Is converting a full matrix to a full matrix slow?

I noticed that converting an already complete matrix to a full matrix is ​​slow:

>> tic; for k = 1:100; x = uint16(ones(10000,100)); end; toc Elapsed time is 0.035748 seconds. >> tic; for k = 1:100; x = uint16(uint16(ones(10000,100))); end; toc Elapsed time is 0.034180 seconds. >> tic; for k = 1:100; x = full(uint16(ones(10000,100))); end; toc Elapsed time is 0.460977 seconds. %%%%% SLOW! 

I tested without uint16 :

 >> tic; for k = 1:100; x = ones(10000,100); end; toc Elapsed time is 0.060028 seconds. >> tic; for k = 1:100; x = full(ones(10000,100)); end; toc Elapsed time is 0.229058 seconds. %%%%% SLOW! 

The same effect.

Why is this? full supposed to convert only a sparse matrix into full matrices. If it is already full, should it not do anything?

EDIT: issparse superfast! I assume this is MEX, is it basically the cost of memory?

MATLAB Version 7.13.0.564 (R2011b) on Mac OS X

+4
source share
2 answers

full works fine.

The slower part is actually ones(10000, 100) ... here's the proof:

 >> tic, for k = 1:100, x = ones(10000,100); end, toc Elapsed time is 0.043143 seconds. >> A = ones(10000,100); >> tic, for k = 1:100, x = full(A); end, toc Elapsed time is 0.000081 seconds. 

full is called with a non-sparse matrix and works quickly, therefore, this is not the reason for the slowdown.

+1
source

Disclaimer: This is my best guess about what is happening, but I don’t know exactly what is happening under the hood of Matlab. Update: An EitanT comment states that my guess is most likely incorrect.

I think the Matlab JIT engine performs optimization in some of these cases, but not all.

When you have a loop in which a variable is created but never used, the JIT engine does not worry about creating this variable again and again. He just does it once. All this will be fast:

 % this calls 'ones' once for i = 1:100, x = ones(10000,100); end 

But if you create a variable and then use it, say, passing it to a function, that variable is created every time. It takes longer, obviously.

 % this calls 'ones' every iteration to pass to `full` for i = 1:100, x = full(ones(10000,100)); end 
+1
source

All Articles