Made a silly mistake, the "for loop no assignment" is of course doing an assignment to the 'ans' variable.
Manipulating large matrices via In-place operations
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I need to perform a sequence of array multiplications, the number of multiplications is unknown, the matrix could be massive and any copying of the data should be minimal.
I've tried a few options including a recusive in-place function I hoped would be equivalent to the single expression version below. From what I can tell reason all the other function calls are slower than the 'single expression' version is that multiple assignment operations are being carried out.
1) What is the fastest way to process a large array recursively? i.e. implement something like:
F(X) = (((((((X*A)*B)*C)*D)*E)*F)*G);
2) Why are subsequent calls to tailTmp much slower?
Example code and run-times below: (note, actual operation .* might be *, or +, etc.)
A = 2*ones(60000,10000);tic;A=A.*A.*A.*A.*A.*A.*A.*A.*A.*A;b = toc;
fprintf('Single expresion: %i\n',b);
A = 2*ones(60000,10000);tic;B=A;for a = 1:9;A= A.*B;end;b = toc;
fprintf('For loop with assignment: %i\n',b);
A = 2*ones(60000,10000);tic;B=A;for a = 1:9;A.*B;end;b = toc;
fprintf('For loop no assignment: %i\n',b);
A = 2*ones(60000,10000);tic;B=A;for a = 1:9;A.*B;end;b = toc;
fprintf('For loop no assignment again: %i\n',b);
A = 2*ones(60000,10000);tic;A=tailTmp(A,1,9);b = toc;%%ran just after saving the tailTmp.m file
fprintf('First call to tail recurrsive in-place: %i\n',b);
A = 2*ones(60000,10000);tic;A=tailTmp(A,1,9);b = toc;
fprintf('Tail recursive in-place again: %i\n',b);
A = 2*ones(60000,10000);tic;A=tailTmp(A,1,9);b = toc;
fprintf('Tail recursive in-place again: %i\n',b);
A = 2*ones(60000,10000);tic;A=A.*A.*A.*A.*A.*A.*A.*A.*A.*A;b = toc;
fprintf('Single expression again: %i\n',b);
A = 2*ones(60000,10000);tic;A=A.^10;b = toc;
fprintf('Thought this would be the fastest...: %i\n',b);
function A = tailTmp(A,c,n)
if(c<=n)
A = A.*tailTmp(A,c+1,n);
end
- Single expresion: 5.292460e-01
- For loop with assignment: 5.156860e+00
- For loop no assignment: 5.857554e+00
- For loop no assignment again: 5.223833e+00
- First call to tail recurrsive in-place: 5.154702e+00
- Tail recursive in-place again: 2.644629e+01
- Tail recursive in-place again: 2.615907e+01
- Single expression again: 5.070650e-01
- Thought this would be the fastest...: 6.410495e+00
Risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!