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

Click For Summary
SUMMARY

This discussion focuses on measuring the computation time of functions in MATLAB. Users recommend using the "cputime" function for elapsed time in seconds and suggest substituting it with specific functions to measure performance. Additionally, the "Profile" tool is highlighted as a comprehensive solution for profiling code execution, with commands such as "profile on" to start profiling and "profile viewer" to analyze results. For precise timing in milliseconds, users are encouraged to explore these built-in MATLAB functions.

PREREQUISITES
  • Familiarity with MATLAB programming environment
  • Understanding of function execution and performance measurement
  • Knowledge of MATLAB's built-in functions, specifically "cputime" and "profile"
  • Basic skills in analyzing profiling results
NEXT STEPS
  • Learn how to use MATLAB's "cputime" function for performance measurement
  • Explore the "Profile" tool in MATLAB for detailed code profiling
  • Investigate the differences between "tic/toc" and "cputime" for timing functions
  • Research best practices for optimizing MATLAB function performance based on profiling results
USEFUL FOR

MATLAB developers, performance analysts, and anyone interested in optimizing function execution time in MATLAB.

hurryon
Messages
9
Reaction score
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   Reactions: rama hasan
Physics news on Phys.org
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   Reactions: rama hasan
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   Reactions: rama hasan
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K