%% Timing functions % % 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, in seconds. x = 1.0 f = @() sin(exp(sqrt(x))); etime = timeit(f) %% Timing portions of code % % To estimate how long a portion of your program takes to run, use the % stopwatch timer functions, tic() and toc(). Invoking tic() starts the % timer, and the next toc() reads the elapsed time. tic() np = 100000; y = zeros(np, 1); for i = 1:np y(i) = sin(exp(sqrt(i))); end etime = toc() %% 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. tic() nrep = 10; for j = 1:nrep np = 100000; y = zeros(np, 1); for i = 1:np y(i) = sin(exp(sqrt(i))); end end etime = toc()/nrep