How to measure running time of a code using MATLAB

Hi. I am new in using MATLAB. I have a thesis about code optimization along with my partner. It was recommended by our adviser to use the software in order t measure the running time of the code. The code is in Java Programming Language. Now, how can I measure the running time of the code using MATLAB?

2 Comments

Hi,
I've coded a function that might help you:
https://uk.mathworks.com/matlabcentral/fileexchange/59187-displayestimatedtimeofloop--tot-toc--curr-ix--tot-iter--
hi; where you want to calculate the time of your code so first you placed tic and toc command in your script.e.g: start tic; code toc

Sign in to comment.

 Accepted Answer

9 Comments

great! i think this is what i am looking for. thanks for answering my question. :)
Very nice function tic and toc i was searching for 2 weeks and i got it to measure compression time for different methods of a medical image.
Thank u Lucas Garcia.
Thanks for the response, I have gained greatly from it.
I'm trying to use tic & toc to measure signal compression & decompression times but the value provided by this function changes for the same program (every time i excute the program, the value changes). Please tell me what should i do.
Use timeit() if your MATLAB is new enough to support it. You can get it from the File Exchange if you have an older version.
Note: even timeit() will vary.
The time measurements you can get will always take into account actual time, which reflects other activity on your system together with factors such as garbage collection or memory allocation potentially taking different amounts of time. It is not possible to get some kind of pure "theoretical" time from MATLAB -- those kinds of measurements have not been meaningful for well over 15 years.
Walter Roberson, Thank u so much. So helpful.
Jay
Jay on 19 Nov 2023
Edited: Jay on 19 Nov 2023
But time of computation is varying when i am running another time the same code. what does it mean ?
Walter Roberson's comment from 29 Sep 2016 is still valid today.

Sign in to comment.

More Answers (1)

beginn your script with tic and end it with toc. At the end the Elapsed time is showed in MATLAB in seconds.

3 Comments

Hi Taha,
Is there a way that the elapsed time can be stored as an output? Since I am recording the outputs from matlab to an excel file, i want it to also write the elapsed time as well (instead of manually reading the time and writing it down).

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!