How to find the CUP time used by each function in my code?

3 vues (au cours des 30 derniers jours)
Benson Gou
Benson Gou le 16 Mai 2021
Dear All,
I want to know the CPU time used by each function of my code. I donot know if it is possible. I donot want to use tic/toc because I need to set up many tic/toc.
Thanks a lot.

Réponse acceptée

Paul Hoffrichter
Paul Hoffrichter le 16 Mai 2021
Modifié(e) : Paul Hoffrichter le 16 Mai 2021
If you need a profile of the relative time spent in every function when running your entire script, then while viewing the main program script in the EDITOR section, you can hit the "Run and Time" button instead of the "Run" button. The resultant summary does not give you CPU time, but only relative times. When you see a function taking up most of the time, you can delve into its hyperlink and get a time profile of the individual lines.
The following link provides an explanation of how to interpret the profiling results.
There is also a cputime function if you really need that. I have never used it and it does not give a complete breakdown of all the functions.
Here is a detailed description of how to profile your code starting at the command window. You set the profile status to on, and run your program. Notice that there is a -timer 'cpu' option. Just be aware that cpu time gives you the sum of the times on all your CPU cores; so the cpu time can be larger than measuring time using timeit or tic/toc.
  4 commentaires
Paul Hoffrichter
Paul Hoffrichter le 16 Mai 2021
Looks like while I was updating my answer, and checking the profile command, @Walter Roberson came up with the same link that you can use from the command window.
Paul Hoffrichter
Paul Hoffrichter le 16 Mai 2021
Modifié(e) : Paul Hoffrichter le 16 Mai 2021
In other IDE's, there is a place to enter arguments to the command line which are used when hitting their Run button. Closest I found in MATLAB is this RUN approach which allows hitting the RUN button with arguments. I just tested to see whether the Run and Time button will pick up the arguments set in the Run button. It does pick up the arguments, so perhaps you can enter your arguments in the Run button, and get the Profile results easily.

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by