3 ms·
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
by imurray 17y ago
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.
- ibsulon 17y agoWhat happens when you run it 1000 times, skipping the first?
- imurray 17y agoIf I use this: http://www.mathworks.com/matlabcentral/fileexchange/18798-timeit-benchmarking-function 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.