Matlab time code

I recorded the function in four different ways and I want to calculate the time.

So far I have been doing this:

tic %//function 1 toc tic %//function 2 toc tic %//function 3 toc tic %//function 4 toc 

But now I want to calculate the time data for each function (say, 100 times) for each, and then calculate the average time spent on each function. How can i do this?

I also read somewhere that the printed time is the elapsed time of the "wall clock", so it will be affected by what else my computer does while the MATLAB program is running.

So is there a better way to do this? I heard that MATLAB has a built-in code profiler with the "profile on" command. Can someone tell me how I can use it?

I also consulted sites: temporary code in MATLAB and Profiler, to find bottlenecks in the code .

Please suggest how to do this many times in a loop. Thanks in advance.

+5
performance timer matlab
source share
3 answers

The best way to use MATLAB code at the time is to use timeit , available in the central MATLAB file exchange.

It was implemented by Steve Eddins, one of the senior developers of MathWorks, and he takes care of a lot of subtleties in determining the time of your code. For example, the code works very differently when it is executed inside a function and not inside a script, and for its proper use the JIT compiler must have a couple of β€œwarm-ups”. It will also run code many times in a loop and take a median.

These things are hard to get without knowing how MATLAB works under the hood, and timeit takes care of these things for you - simple tic and toc applications do not.

Using the profiler, as other answers suggested, is problematic because it disables many aspects of the JIT compiler and will not work at the same speed as in normal mode. The profiler does an excellent job of which parts of your code take a relatively large fraction of the time, that is, they find bottlenecks, but are not intended to provide you with realistic realistic timings.

Please note that in the latest version (R2013b) timeit is available as part of the main MATLAB and does not need to be received from File Exchange.

For example, by the time of your one function with an input argument x of 64, you would enter:

 myfun = @()one(64); timeit(myfun); 

This is done in order to make the function descriptor of your function one (which ensures that the code is executed inside the function, as mentioned above), then passes this function descriptor to timeit . The result is a timeit estimate of the time taken to execute the code.

+15
source share

A profiler is one of the features, but it will significantly slow down your code. Alternatively, you can store the toc value in your loop or after each function call.

 t(i) = toc 

and then compare these values, calculate the average value or whatever, as if you were dealing with other vectors.

+3
source share

Using a profiler is almost as simple as tic / toc:

 profile on; for i=1:N your_function() end profile viewer; 

If your 4 functions are independent and do not affect each other, you can also profile them all in one block:

 profile on; for i=1:N your_function1() your_function2() your_function3() your_function4() end profile viewer; 

The profiler allows you to see the processing time for each individual line of code. You can either set the wall clock or processor time, the default value is cpu-time. See the profile documentation for how to change this.

EDIT: What I like about the profiler is that it gives you a breakdown of each sub-function processing time - hence it is a great way to identify bottlenecks in larger processes. This is probably not so much a precedent here.

+1
source share

All Articles