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

2 views (last 30 days)

Show older comments

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 Comment

Matt J
on 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.

### Answers (1)

Bus141
on 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 Comments

Walter Roberson
on 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
on 9 Nov 2015

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!