Three methods for calculating program running time in MATLAB

Source: Internet
Author: User

From: http://apps.hi.baidu.com/share/detail/47158063

Three methods for calculating program running time in MATLAB

We often need to calculate how long our program runs, so as to compare the execution efficiency of the program. Of course, this is meaningless for small programs that only have a few seconds, but it is very important for large programs.

Next, let's talk about three common methods for calculating the program running time in MATLAB!

Note:The three methods may have different results due to different usage principles!

1. Combination of TiC and TOC (most used)

Calculate the running time between the program TiC and TOC. Its classic format is

  1. TIC
  2. ..........
  3. TOC

Copy code

In other words, when the program encounters tic, Matlab automatically starts timing and runs to TOC automatically calculates the time between this time and the last tic. This is a bit difficult. The following is an example.

  1. % By dynamic of MATLAB Technology Forum
  2. % See also http://www.matlabsky.com
  3. % Contact me matlabsky@gmail.com
  4. % 2009-08-18 12:08:47
  5. CLC
  6. TIC; % tic1
  7. T1 = clock;
  8. For I = 1:3
  9. TIC; % tic2
  10. T2 = clock;
  11. Pause (3 * rand)
  12. % Calculates the time of the last tic, that is, the time of each loop.
  13. Disp (['toc calculate the nth ', num2str (I), 'cycle runtime:', num2str (TOC)]);
  14. % Calculate the time of each loop
  15. Disp (['etime calculated nth ', num2str (I), 'cycle runtime:', num2str (etime (clock, T2)]);
  16. % Total running time of the computing program
  17. Disp (['time when the etime computing program starts to run: ', num2str (etime (clock, T1)]);
  18. Disp ('========================================== = ')
  19. End
  20. % Calculate the time from this time to tic2. Because TiC is at I = 3 of the for loop, the time of the last loop is calculated.
  21. Disp (['toc calculate the last cyclic runtime ', num2str (TOC)])
  22. Disp (['total running time of the etime program: ', num2str (etime (clock, T1)]);

Copy code

The running result is as follows. You can analyze it yourself.

  1. 1st running times of TOC computing: 2.5628
  2. Etime: 1st running cycles: 2.562
  3. The time for running the etime computing program from the start to the current: 2.562
  4. ==============================================
  5. 2nd running times of TOC computing: 2.8108
  6. Etime: 2nd running cycles: 2.813
  7. The time for running the etime computing program from the start to the current: 5.375
  8. ==============================================
  9. 3rd running times of TOC computing: 2.0462
  10. Etime: 3rd running cycles: 2.046
  11. The time for running the etime computing program from the start to the current: 7.421
  12. ==============================================
  13. The last running time of TOC computing is 2.0479.
  14. Total running time of the etime program: 7.421

Copy code

2. The etime (T1, T2) is used with clock.

To calculate the time difference between t1 and t2. It is obtained by calling the Windows system clock for time difference calculation.

  1. T1 = clock;
  2. ...........
  3. T2 = clock;
  4. Etime (t2, T1)

Copy code

As for the example, I will not give it, because the etime function is used in the above example.

3. Complete the cputime function.

The usage method is similar to etime, but it is calculated based on the CPU clock speed. Unlike the previous principle, the usage format is as follows:

  1. T0 = cputime
  2. .............
  3. T1 = cputime-t0

Copy code

The above three methods can be used to calculate the program running time, But MATLAB officially recommends TiC/TOC combination, when timing the duration of an event, use the TIC and TOC functions instead of clock or etime.

You can choose your preferred option, but when using TiC/TOC, you must note that TOC calculates the time between TiC and the last running tic, not the first tic, not the second .....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.