Timeit on pause(2) returns weird ouput

3 views (last 30 days)
Torkel Loman
Torkel Loman on 28 Feb 2022
Answered: Peter Perkins on 2 Mar 2022
I am trying to get the timeit function to work.I read https://uk.mathworks.com/help/matlab/ref/timeit.html and get the example:
X = rand(100);
f = @() svd(X);
t1 = timeit(f)
I want to try this using the pause(2), since this should take about 2 seconds:
g = @() pause(2);
t2 = timeit(g)
the output is
t2 =
3.4639e-06
which is a lot less than the expected 2 seconds. Clearly I am missunderstanding something (which probably makes my more serious attempts at using timeit also wrong), but what?

Answers (1)

Peter Perkins
Peter Perkins on 2 Mar 2022
The issue is pause, not timeit:
"STATE = pause(...) returns the state of pause previous to processing the input arguments."
>> t2 = timeit(g,0)
t2 =
2.000208413333

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!