Set or check a wall-clock timer. Calling
tic
without an output argument sets the timer. Subsequent calls totoc
return the number of seconds since the timer was set. For example,tic (); # many computations later... elapsed_time = toc ();will set the variable
elapsed_time
to the number of seconds since the most recent call to the functiontic
.If called with one output argument then this function returns a scalar of type
uint64
and the wall-clock timer is not started.t = tic; sleep (5); (double (tic ()) - double (t)) * 1e-6 5Nested timing with
tic
andtoc
is not supported. Thereforetoc
will always return the elapsed time from the most recent call totic
.If you are more interested in the CPU time that your process used, you should use the
cputime
function instead. Thetic
andtoc
functions report the actual wall clock time that elapsed between the calls. This may include time spent processing other jobs or doing nothing at all. For example,tic (); sleep (5); toc () 5 t = cputime (); sleep (5); cputime () - t 0(This example also illustrates that the CPU timer may have a fairly coarse resolution.)