Figuring cputime and runing time of X=inv(A)*b, and the X=A\b.
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Matt J
2015년 11월 9일
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.
답변 (1개)
Bus141
2015년 11월 8일
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
Walter Roberson
2015년 11월 9일
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
2015년 11월 9일
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 \ )
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!