Time elapsed between date vectors
While you can represent dates and times as date vectors, it is recommended
that you use
datetime values to
represent points in time, and
calendarDuration values to
represent elapsed times.
e = etime( returns the number
of seconds between two date vectors or matrices of date vectors,
Compute Elapsed Time
Compute the time elapsed between a specified time and the current time.
Specify the initial date and time as the start of January 2020. Convert it to date vector form.
t1 = datevec('2020-01-01')
t1 = 1×6 2020 1 1 0 0 0
Determine the current date and time. The
clock function returns the current date and time as a date vector.
t2 = clock
t2 = 1×6 103 × 2.0220 0.0020 0.0260 0.0110 0.0580 0.0201
You can compute the number of seconds between
t2 by using
e = etime(t2,t1)
e = 6.8040e+07
However, this method is not recommended for measuring the time it takes to run your code.
t2,t1 — Date vectors
1-by-6 vector |
Date vectors, specified as 1-by-6 vectors or
m full date vectors in the
[Year Month Day Hour Minute Second].
[2012 03 27 11 50 01]
To time the duration of an event, use the
tocfunctions instead of
clockfunction is based on the system time, which can be adjusted periodically by the operating system, and thus might not be reliable in time comparison operations.
etime does not account for the following:
Daylight saving time adjustments.
Differences in time zones.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.