[英]How do I calculate execution time of Matlab Code for variable input?
我想在以下條件下計算Matlab代碼的執行時間:我編寫了一個名為somecode
的Matlab函數,該函數具有輸入變量counter
。 我想計算每個輸入值的執行時間。 最初, counter=20
,因此somecode
的輸入為20。然后, counter
在每次迭代中增加20,直到達到500。我想知道somecode
對counter
每個值(20到500) somecode
的時間。 。 我在哪里需要使用tic
, toc
命令?
counter=20;
while (counter<=500)
somecode(counter);
counter=counter+20;
end
請提出任何解決方案。 謝謝
最初的想法是,我會使用for
循環執行類似的操作。 沒什么花哨的,但是應該給運行時一個思路。
% Create a vector for the entries of counter and for the runtimes.
% (Make them column vectors for displaying with table)
big_counter = (20:20:500)';
somecode_times = zeros(size(big_counter));
% Loop over the entries of counter
for ii = 1:length(big_counter)
counter = big_counter(ii);
tic;
somecode(counter);
% Save the runtime to somecode_times
somecode_times(ii) = toc;
end
% Display the times in a table by uncommenting
%table(big_counter, somecode_times)
% Otherwise show the results side by side
display([big_counter, somecode_times])
plot(big_counter, somecode_times,'+-')
xlabel 'counter'
ylabel 'time'
title 'time taken to run somecode(counter)'
輸出示例:
20.0000 0.0008
40.0000 0.0017
60.0000 0.0047
80.0000 0.0072
100.0000 0.0107
120.0000 0.0186
140.0000 0.0297
160.0000 0.0496
180.0000 0.0658
200.0000 0.0949
220.0000 0.1743
240.0000 0.1734
260.0000 0.2313
280.0000 0.2767
300.0000 0.3187
320.0000 0.3959
340.0000 0.4679
360.0000 0.6698
380.0000 0.7474
400.0000 0.9920
420.0000 0.9221
440.0000 1.1148
460.0000 1.2610
480.0000 1.3960
500.0000 1.6945
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.