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.
Sam roberts
source share