A Matlab/Octave example of inv being slower and less accurate:
>> A = randn(100); x = randn(100,1); b = A*x;
>> tic; x1 = inv(A)*b; toc
Elapsed time is 0.142381 seconds.
>> tic; x2 = A\b; toc
Elapsed time is 0.000736 seconds.
>> max(abs(x-x1))
ans =
7.1054e-14
>> max(abs(x-x2))
ans =
4.8406e-14
Edit: Actually this is a warning about timing stuff. The inv function hadn't "warmed up". On a second run both times drop, and the difference is much smaller:
>> tic; x1 = inv(A)*b; toc
Elapsed time is 0.000737 seconds.
>> tic; x2 = A\b; toc
Elapsed time is 0.000471 seconds.
which warms up the function, averages over repetitions and tries to account for various overheads, the ratio states about the same (1:1.5 ish). I would normally use timeit(), but for a quick post, I was being sloppy.
Comments
A Matlab/Octave example of inv being slower and less accurate:
Edit: Actually this is a warning about timing stuff. The inv function hadn't "warmed up". On a second run both times drop, and the difference is much smaller:What happens when you run it 1000 times, skipping the first?
If I use this:
http://www.mathworks.com/matlabcentral/fileexchange/18798-ti...
which warms up the function, averages over repetitions and tries to account for various overheads, the ratio states about the same (1:1.5 ish). I would normally use timeit(), but for a quick post, I was being sloppy.