MATLAB Answers

whats is presice compute time in matlab

2 views (last 30 days)
Abolfazl Nejatian
Abolfazl Nejatian on 30 Jan 2020
Hi every one,
i just want to check different parts of my code's computation time, so i use tic toc function.
the results are varied than i use this loop and find out strange results
for i=1:13
tic,toc;
end
  • Elapsed time is 0.000037 seconds.
  • Elapsed time is 0.000013 seconds.
  • Elapsed time is 0.000014 seconds.
  • Elapsed time is 0.000013 seconds.
  • Elapsed time is 0.000121 seconds.
  • Elapsed time is 0.000002 seconds.
  • Elapsed time is 0.000001 seconds.
  • Elapsed time is 0.000001 seconds.
  • Elapsed time is 0.000001 seconds.
  • Elapsed time is 0.000000 seconds.
  • Elapsed time is 0.000000 seconds.
  • Elapsed time is 0.000000 seconds.
  • Elapsed time is 0.000000 seconds.
the question is these different elapsed times how created?
and why the elapsed time sequence is descending?
kind regard,
Abolfazl

  0 Comments

Sign in to comment.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 30 Jan 2020
These fluctuations are "kind of expected", your computer does a lot of other stuff in addition to running matlab which gives the elapsed time a little bit of variation. To get good estimates of the differences in running-times of code-snippets the general rule is to take the average time over some reasonable number of itterations.
For your case a better option might be to run your code with profiling on - that will give you a very precise tracking of what lines takes how much time, chech the help and documentation for profile.
Matlab used to have functions for cpu-time used, but that was discontinued after multithreading and other modern capacities was introduced.
HTH

  3 Comments

Rik
Rik on 30 Jan 2020
The timeit function should probably be mentioned in this context, as it automatically determines the reasonable number of iterations and calculates the median time elapsed.
Also, these small variations are so small they can also be rounding errors, and the downward trend may be caused by JIT optimization.
Walter Roberson
Walter Roberson on 30 Jan 2020
profiling disables some optimizations, so profiled code will not perform as efficiently as regular code. profiling is good for getting indications of which part of your code are taking the most time, but profiling is not good for precisely timing how long something takes.
You should look at timeit() , which uses algorithms to do better estimating of the CPU time required for something.
Abolfazl Nejatian
Abolfazl Nejatian on 4 Feb 2020
dear Walter,
thank you for your response,
i try the timeit() function and it was work for me.
actually i need to evaluate small time from a WebSocket server which are in the millisecond range.
but most of the time evaluation method didn't work efficiently and i finally send a big bunch of data and evaluate the processing time by dividing data size to the total computation time to understanding the computation time per sample.

Sign in to comment.

Sign in to answer this question.


Translated by