clear all;

n=1000;

A=rand(n,n);
[L,U,P] = lu(A);

ntry=10;

for i= 1:ntry
    b=rand(n,1);
    
    tic; %inv(A)
    x = inv(A)*b;
    ti(i)=toc;
    
    tic; %A\b
    x = A\b;
    tb(i)=toc;
    
    tic; %compute LU decomposition each time
    [L,U,P] = lu(A);
    x = U\(L\(P*b));
    tlufull(i)=toc;
    
    tic; %compute LU at beginning, only once
    x = U\(L\(P*b));
    tlu(i)=toc;
    
end

mti=sum(ti)
mtb=sum(tb)
mtlufull=sum(tlufull)
mtlu=sum(tlu)