I have written down a function in 4 different ways and I want to time it .
Up-to now I have been doing this thing :
tic
%//function 1
toc
tic
%//function 2
toc
tic
%//function 3
toc
tic
%//function 4
toc
But now I want to compute the timing data for each function for (say 100 times) each and then compute the average time spent on each function. How can I do so?
Also I read somewhere that the time printed is the elapsed “wall clock” time – so it will be affected by whatever else my computer is doing whilst the MATLAB program was running.
So is there a better way of doing it ?? I have heard there is a MATLAB built in code-profiler with the command "profile on". Please can anyone suggest me the way in which I can use it?
I have also consulted the sites : Timing code in MATLAB and Profiler to find code bottlenecks.
Please suggest how to do this many times in a loop. Thanks in advance.
To measure the time required to run a function, use the timeit function. The timeit function calls the specified function multiple times, and returns the median of the measurements. It takes a handle to the function to be measured and returns the typical execution time, in seconds.
Description. start( t ) starts the timer t . If t is an array of timers, start starts all the timers. The start function sets the Running property of the timer to 'on' , executes the StartFcn callback, and initiates TimerFcn callback.
Description. shifted_data = delayseq( data , delay ) delays or advances the signal in data by the number of samples specified in delay . Positive values of delay delay the signal, while negative values advance the signal. Noninteger values of delay represent fractional delays or advances.
The best way to time MATLAB code is to use timeit, available from the MATLAB Central File Exchange.
It was implemented by Steve Eddins, one of the senior developers at MathWorks, and it takes care of a lot of subtleties in timing your code. For example, code runs very differently when it's executed within a function rather than within a script, and it needs to have a couple of "warm-up" runs in order to take proper advantage of the JIT compiler. It will also run the code many times in a loop, and take the median.
These things are difficult to get right without knowing a fair amount about how MATLAB works under the hood, and timeit
takes care of these things for you - simple applications of tic
and toc
do not.
Using the profiler, as other answers have suggested, is problematic as it switches off many aspects of the JIT compiler, and will not run at the same speed as it does normally. Profiler does a great job of telling you which portions of your code take a relatively large proportion of time, i.e. discovering bottlenecks, but it's not intended for giving you actually realistic timings.
Note that in the most recent version (R2013b), timeit
is available as part of core MATLAB, and does not need to be obtained from the File Exchange.
For example, to time your function one
with the input argument x
equal to 64, you would type:
myfun = @()one(64);
timeit(myfun);
What this does is to make a function handle to your function one
(which makes sure that the code is executed inside a function, important as mentioned above), then passes this function handle into timeit
. The output is timeit
's estimate of the time taken to execute the code.
The profiler is one possibility, but it will slow down your code significantly.
Alternatively you could store the toc
value within your loop or after every function call.
t(i) = toc
and then compare these values, compute the mean or whatever, like you would deal with other vectors.
Using the 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 don't influence each other, you can also profile all of them in one block:
profile on;
for i=1:N
your_function1()
your_function2()
your_function3()
your_function4()
end
profile viewer;
The profiler will let you have a look at the processing times for each single line of code.
You can either benchmark wall-clock or cpu-time, the default is cpu-time. See the profile
documentation for how to change that.
EDIT: What I like about the profiler is, that it gives you a breakdown of each subfunction's processing time - hence it's a great way to spot bottlenecks in larger processes. That's probably not so much the use case here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With