Computataion Speed in Matlab using 3 nested 4 loops
显示 更早的评论
1. Compute the overhead CPU time—i.e., the time required just to process the loop bookkeeping—required to process a set of three nested MATLAB for loops where each loop runs for 1000 cycles. Run three trials and report the elapsed CPU time in seconds for each trial and for the average of the three trials.
2. Modify your program from Problem 2 to compute the time required to perform 1 billion scalar additions in MATLAB. Your answer should be only the net time required for the additions—i.e., subtract out the average loop overhead time determined in Problem 2. Run three trials and report the elapsed CPU time in seconds for each trial and for the average of the three trials.
I really have no idea how to go about these problems so any help would be greatly appreciated.
回答(1 个)
Walter Roberson
2011-9-29
Let C represent the computing operation you want to do.
tic;
for K = 1 : 1000
C;
end
t1 = toc
will report on the total loop time including the loop overhead.
Now
tic;
for K = 1 : 100
C; %1
C; %2
C; %3
C; %4
C; %5
C; %6
C; %7
C; %8
C; %9
C; %10
end
t2 = toc
will do the same amount of computation, and will have the same fixed loop initialization overhead, but will have only 1/10th of the per-iteration loop overhead because it does only 1/10th as many loops.
per_iteration = (t2 - t1) / (1000-100);
per_loop = t2 - 1000*per_iteration;
Question 2 is a hoax: MATLAB additions get faster if you do enough of them at one time.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!