Figuring cputime and runing time of X=inv(A)*b, and the X=A\b.

5 vues (au cours des 30 derniers jours)
N/A
N/A le 8 Nov 2015
There is a difference between using X=inv(A)*b, and the X=A\b. The second one is faster, but only when the set of linear of equations is of huge size. I need to find out how big the matrix is when the difference of calculation of time can be seen? I want to use cputime to randomize the difference size matrices and to solve equations by both methods, and checking the time to finish the calculation.
A=rand(n) B=rand(n,1)
t0=cputime .... ... .. RunningTime=cputime - to
  1 commentaire
Matt J
Matt J le 9 Nov 2015
The second one is faster, but only when the set of linear of equations is of huge size.
And, only when b has a small number of columns:
d=30;
N=1e6;
A=rand(d);
b=rand(d,N);
tic
A\b;
toc
tic;
inv(A)*b;
toc
Elapsed time is 0.248753 seconds.
Elapsed time is 0.145203 seconds.

Connectez-vous pour commenter.

Réponses (1)

Bus141
Bus141 le 8 Nov 2015
I am not exactly sure what you are trying to do but it seems you are trying to time each operation to find at which point the other method is quicker. My thought is to just loop the matrix sizes.
for i=1:100
A=rand(i);
b=rand(i,1);
tic
X=inv(A)*b;
timing(i,1)=toc;
tic
X=A\b;
timing(i,2)=toc;
end
  3 commentaires
Walter Roberson
Walter Roberson le 9 Nov 2015
This is subject to noise. If you have a reasonably new version, use timeit for the timing. If not then get timeit from the file exchange.
for i = 1:100; A = rand(i);b = rand(i,1); timing(i,1) = timeit(@() inv(A)*b); timing(i,2) = timeit(@() A\b); end
You will need to run further than 100 for the second method to be faster. I am running further tests now but they are taking a while.
Walter Roberson
Walter Roberson le 9 Nov 2015
In my tests, the results vary from run to run, indicating that the exact random numbers make a difference. The highest value where the two times were equal (or pretty close) that I observed was about 65, but 45-ish was more common, and the highest size at which I relatively consistently saw inv() being faster was the 4 x 4 case (and that case only, with 1x1, 2x2, 3x3 being consistently faster when using \ )

Connectez-vous pour commenter.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by