Friday, March 30, 2018

When the tic-toc measurement is not reasonable?

Bismillah. In the Name of Allah, is the start of all things good. We too shall start with it.
(Said Nursi d. 1960)

I need to measure the computing time of my program for several matrix order. Tic-toc is perhaps very useful function provided in MATLAB for this task. But sometimes (not so much), this tic-toc gave me unreasonable time record for my program. Here for the example ...

Matrix Order |   1    |   2    |    3   | ...
Time         | 0.0122 | 0.0023 | 0.0015 | ...

It’s very weird situation. How can larger order matrix (2) takes faster time than (1). And it is also happened in other similar situations. I attempted any possible clue in getting rid of this problem: single thread computation, variable pre-declaration, running one-by-one instead of loop, etc. But nothing worked.

Alhamdulillah. The problem is solved after seeing this fraction of Mathworks documentation.
Sometimes programs run too fast for tic and toc to provide useful data. If your code is faster than 1/10 second, consider measuring it running in a loop, and then average to find the time for a single run.
This statement exactly matches with my situation, i.e. the time computation is faster than 0.1 second. Hence, I rewrite my code and adding a loop.

Ns=1000;
tic
for i=1:Ns
    Ts=phmaxsys(T1,T2,T3)
end
t=toc/Ns

This code implies that function phmaxsys needs to be executed Ns times then the time measured is divided with Ns in order to get the computing time of single iteration. By this way, tic-toc measurement provides reasonable time computation as expected.

Matrix Order |   1    |   2    |    3   | ...
Time         | 0.0082 | 0.0133 | 0.0545 | ...

Alhamdulillah. More detailed description can be seen as follows. Thank u.

REFERENCES
https://www.mathworks.com/help/matlab/matlab_prog/measure-performance-of-your-program.html
https://www.mathworks.com/help/matlab/ref/tic.html

No comments:

Post a Comment