How can I modify the following code:
8 views (last 30 days)
Show older comments
function [Q,R]=mgs(A)
[m,n]=size(A);
V=A;
Q=zeros(m,n);
R=zeros(n,n);
for i=1:n
R(i,i)=norm(V(:,i));
Q(:,i)=V(:,i)/R(i,i);
for j=i+1:n
R(i,j)=Q(:,i)'*V(:,j);
V(:,j)=V(:,j)-R(i,j)*Q(:,i);
end
end
such that the outputs are Q,R, the number of floating point operations (num_flops), and execution time (t_exec). Floating point operations are +,−, /,×,p. Test it using n × n random matrix A. Increase the size of n and plot a graph with n as x axis and num_flops as y axis. Do you see a trend where num_flops is in the order of n^3 ? Plot a graph with num_flops as x axis and execution time (t_exec) as y axis.
0 Comments
Answers (1)
Walter Roberson
on 17 Oct 2015
You have not been able to measure floating point operations in MATLAB for quite a number of years. MATLAB optimizes for execution speed, not for reducing the number of floating point operations. The two can be different due to Out Of Order Execution and Predictive Execution, and executing with multiple cores: it can be more efficient to do extra work than to go through the overhead of reducing the floating point operations to the theoretical minimum.
With sufficiently large arrays, MATLAB will call highly optimized libraries which can take advantage of multithreading and multicores and the quirks of the instruction set of the particular CPU you are executing on.
For these reasons, experimentally measuring Floating Point Operations has become pretty much obsolete.
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!