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

Click For Summary

Discussion Overview

The discussion focuses on how to measure the computation time of a function in Matlab, exploring various methods and tools available within the software. The scope includes practical applications and technical explanations related to performance measurement in programming.

Discussion Character

  • Technical explanation, Exploratory

Main Points Raised

  • One participant inquires about methods to calculate the computation time of a function in Matlab, suggesting that Matlab may have built-in functions for this purpose.
  • Another participant points out that while "tic" and "toc" measure elapsed time in seconds, they seek a method to obtain the time in milliseconds.
  • A different participant proposes using "cputime" as a straightforward way to measure elapsed time, providing an example of its usage with the "bench" function.
  • Another participant mentions the "Profile" tool in Matlab, explaining how to activate it and view results, indicating it is specifically designed for performance profiling.

Areas of Agreement / Disagreement

Participants present multiple methods for measuring computation time, with no consensus on a single preferred approach. Each method has its own merits and applications, leaving the discussion open-ended.

Contextual Notes

Participants do not clarify the limitations or specific contexts in which each method may be more appropriate, nor do they address potential dependencies on function complexity or system performance.

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
4K
  • · 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
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 10 ·
Replies
10
Views
4K