[Matlab] How to obtain the computation time of a function

In summary, the conversation is about calculating the computation time of a function in Matlab. The person asking the question wants to know if there is a way to obtain the time in milliseconds, while another person suggests using "cputime" for this purpose. Lastly, someone mentions the tool "Profile" in Matlab, which can be turned on and viewed to see the results of execution.
  • #1
hurryon
9
2
Hi, all.

I have a function in Matlab. I'd like to calculate the computation time of the function. How can I obtain the time? I think Matlab must include related functions as its internal functions.

Thanks in advance.
 
  • Like
Likes rama hasan
Physics news on Phys.org
  • #2
Thanks, but "tic" and "toc" provide the elapsed time in seconds. Do you know a way that provides the elapsed time in milliseconds ?

Thanks in advance.
 
  • Like
Likes rama hasan
  • #3
A quick and easy way to do this is to use "cputime" to calculate the elapsed time. For instance, suppose that you wanted to determine how long it takes for the MATLAB benchmark to run. Then you'd use the following:

Code:
time1 = cputime;
bench;
cputime - time1

Obviously, you can substitute "bench" for your function to determine how long it takes to run. See "doc cputime" for more information.
 
  • Like
Likes rama hasan
  • #4
Hello,
The tool in MATLAB built for this purpose is: "Profile".
In the MATLAB search at: "http://www.mathworks.com/"
and typing "profile" will bring up the information.

Main commands are turning it on:
profile on
and viewing the results (after some execution):
profile viewer

kind regards.
 
Last edited by a moderator:

1. How can I measure the computation time of a function in Matlab?

In order to obtain the computation time of a function in Matlab, you can use the built-in function tic and toc. These functions create a timer that starts when tic is called and stops when toc is called. The difference between the two values gives you the computation time.

2. Can I measure the computation time of a specific section of code?

Yes, you can use the same tic and toc functions to measure the computation time of a specific section of code. Simply place tic before the code you want to measure and toc after it. The difference between the two values will give you the computation time for that section of code.

3. Is there a way to display the computation time in a specific unit?

Yes, by default, the computation time is displayed in seconds. However, you can use the toc function with an output argument to specify the unit of measurement. For example, time = toc('ms') will give you the computation time in milliseconds.

4. Can I measure the computation time of a function multiple times?

Yes, you can use a loop to run the function multiple times and measure the computation time each time using tic and toc. This can be useful for comparing the efficiency of different algorithms or for finding the average computation time.

5. Can I save the computation time data for later use?

Yes, you can save the computation time data by storing the output of toc in a variable or writing it to a file. This can be helpful for tracking the performance of your code over time or comparing it with data from other runs.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
822
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
994
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
Back
Top