gogoWebsite

Simulation Fundamentals] [MATLAB] Timing Details: Five Timing Methods Suggested Uses, Basic Advantages and Disadvantages

Updated to 4 months ago

MATLAB timing tool is to improve the efficiency of the program is a key aid, but I always need to re-query each time I use the method of use, and often because of the long interval between the use of personal experience and the need to re-accumulate. In order to avoid this problem, and therefore decided to organize here, available for their own benefit to everyone.The content inevitably contains errors and deficiencies, welcome your criticism and correction. If you think it is useful, you may want to click a like yo~

MATLAB Timing

  • I. Recommendations for the use of the five timing methods
  • II. Specific methods of use and basic advantages and disadvantages
    • 1. Execution time display in the command history window
    • 2. Editor's "run and clock" tool
    • 3. "tic + toc" combination
    • 4. "clock + etime" combination
    • 5. “cputime”: CPU time
  • III. Comparison of time consumption of the last three timing methods (MATLAB program)

MATLAB comes with five timing tools, namely "show execution time" in the command history, "run and time" module in the editor, "tic+toc The "tic+toc" combination, the "clock+etime" combination, and "cputime". These methods are described below.

I. Recommendations for the use of the five timing methods

To make it easier to read and use, the conclusion is put directly. For specific usage and analysis, you can read further down.

serial number Timing method Recommendations for use
1 The command history window shows the execution time Simply set it to be normally open
2 Editor's "run and time" tool Available only if you care about the execution time of each function
3 "tic+toc" combination This method can be used when you are interested in the execution time of a section of the program rather than a specific function, and it is recommended that you use this method first.
4 "clock+etime" combination Can be used when you are concerned about the execution time of a program rather than a specific function. The data is arranged by "year, month, day, hour, minute, second", which is easy to understand but takes longer than the combination of "tic+toc". It is not recommended to use it only for timing.
5 “cputime” The output is CPU time, which is not in seconds and not real time, so it is not recommended to use it.

II. Specific methods of use and basic advantages and disadvantages

1. Execution time display in the command history window

Usage: Select "Command History" in "Preset" and check "Show Execution Time" to display the execution time of each history program.
vantage: Checking the box once is sufficient, no additional settings are required and no additional time is consumed.
drawbacks: Only the total time of the current program can be displayed, there is no more detailed time display.

2. Editor's "run and clock" tool

Usage: The most common timing tool is the editor's own "run and time" tool, click to run and output the running time of each function.
vantage: This tool directly counts the usage time of each function (including the main function), which helps to observe the function running time in the program, and then find the bottleneck.
drawbacks: It is not easy to keep direct statistics on how long a particular segment of the program has been running.

3. "tic + toc" combination

Usage: Enter "tic;" where you want to start the timer and "toc;" where you want to end the timer. Here "toc" is the actual running time of the program from the start of "tic" to "toc", in seconds. Therefore, if you want to count the total running time of a program in a certain loop, you can add up the value of "toc". An example is shown below:

t = 0;
for cycle_no = 1: 10
    tic;
    pause(0.1); % The code to count the time, here is an example of a program that pauses for 0.1 seconds.
    t = t + toc; % toc is the time in seconds from tic to here.
    pause(0.2);  % Other code, here is an example of a program that pauses for 0.2 seconds
end
% At the end of the run, t = 1.0359

If nesting of tic and toc is involved, the following method can be used to avoid tic and toc matching confusion:

t0 = tic;
pause(1)
t1 = tic;
pause(1)
toc(t1); % Execution time from t1 to here
pause(1)
toc(t0); % Execution time from t0 to here
% Run results:
% 1.017749 seconds have elapsed.
% 3.029855 seconds have elapsed.
% 
% Note: If you do not assign "toc" to a variable, it will directly output the elapsed time, as in this example. For details, use "edit toc".

If it involves timing a step in a loop, you can use the following method:

t_delay = 0.1;
tCount1 = 0;
t0 = tic;
for i = 1:10
	for j = 1:10
		pause(t_delay)
		t1 = tic;
		pause(t_delay)
		tCount1 = tCount1 + toc(t1); % Execution time from t1 to here
    end
end
tCount0 = toc(t0); % Execution time from t0 to here
fprintf(['Time 0 is ', num2str(tCount0), ' seconds.\n'])
fprintf(['Time 1 is ', num2str(tCount1), ' seconds.\n'])
% Run results:
% Time 0 is 21.7805 seconds.
% Time 1 is 10.8958 seconds.

vantage(i) it is possible to count the total elapsed time of a segment of the program, and (ii) the additional elapsed time of the timing program itself is less than that of the latter two methods.
drawbacks: Additional time-consuming.

4. "clock + etime" combination

UsageThe record of clock is the current moment, in the form of a 1x6 dimensional double vector, such as "[2020, 9, 30, 16, 25, 29. 1760]", several data represent year, month, day, hour, minute and second. When using, in the need to start the position of the clock input "t1 = clock;", in the end of the position of the clock input "t2 = clock;", with "etime(t2, t1) " can get the time difference between the two, the unit is seconds. The example is as follows:

t = 0;
for cycle_no = 1: 10
    t1 = clock;
    pause(0.1); % Code to count time
    t2 = clock;
    t = t + etime(t2, t1); % These two lines of code can also be simplified directly to "t = t + etime(clock, t1);"
    pause(0.2);  % Other codes
end
% At the end of the run, t = 1.0530, which is slightly longer than the "tic +toc" combination

vantageThe result of clock is obvious and easy to understand, which makes it easy to extract the moments.
drawbacks: The additional time required is longer than for the "tic + toc" combination, and is about 15 times longer than for the "tic + toc" combination. (The multiplier is derived from several simulations, and the corresponding program is attached in Part III.)

5. “cputime”: CPU time

Usage: "cputime" outputs CPU time, not real time, i.e. the unit is not seconds. When you use it, input "t1 = cputime;" at the position where you want to start timing, input "t2 = cputime;" at the position where you want to end timing, and use "t2 - t1 " to get the time difference between the two. The example is as follows:

t = 0;
for cycle_no = 1: 10
    t1 = cputime;
    pause(0.1); % Code to count time
    t2 = cputime;
    t = t + t2 - t1; % These two lines of code can also be reduced to "t = t + cputime - t1;"
    pause(0.2);  % Other codes
end
% end of run, t = 0.1406

It is also clear from t < 1 that theCPU time and real time are not the same
vantage: It is possible to count the elapsed time of a particular segment of a program. (No other use for this method has been identified.)
drawbacks(i) the CPU time is different from the real time, and (ii) the extra time required is longer than the "tic + toc" combination, which is about 5 times as long as the "tic + toc" combination. (The multiplier is derived from multiple simulations, and the corresponding program is attached in Part III.)

III. Comparison of time consumption of the last three timing methods (MATLAB program)

%% Three Methods of Timing: Time Consuming Comparison
%Results
% Method 1: tic + toc
  Speed: fast
  Weakness: each `toc` only match with its exactly last 'tic'
Method 2: clock + etime
  Speed: medium, around 1/15 of Method 1
  Weakness: just slow
Method 3: cputime
  Speed: medium, around 1/5 of Method 1
  Weakness: cputime is not the real time we use in daily life

  
N = 1e6;

% Method 1: tic + toc
a = tic;
t1 = 0;
for i = 1: N
    tic;
    t1 = t1 + toc;
end
toc(a);

% Method 2: clock + etime
b = tic;
t2 = 0;
for i = 1: N
    t0 = clock;
    t2 = t2 + etime(clock, t0);
end
toc(b);

% Method 3: cputime
c = tic;
t3 = 0;
for i = 1: N
    t0 = cputime;
    t3 = t3 + cputime - t0;
end
toc(c);

% Run results:
% 0.594472 seconds have elapsed.
% 9.540728 seconds have elapsed.
% 3.595892 seconds have elapsed.