- [Instructor] Welcome to the next video of Section Eight,
calculating the execution time for a command.
In the previous video,
we learned about monitoring disk usage.
In this video, we'll learn how to calculate
the execution time for a command.
While testing an application's efficiency
or comparing different algorithm's to solve a given problem,
the execution time taken is very critical.
A good algorithm should execute in a minimum amount of time.
Let's see how to calculate the execution time.
Let's see how to do it.
To measure the execution time, just pre-fix "time"
to the command you want to run, for example.
The command will execute and it's output will be shown.
Along with the output, the time command
appends the time taken in standard error.
An example is as follows.
It will show real, user, and system times for execution.
An executable binary of the time command
is available at /user/bin/time,
as well as a shell built in named time exists.
When we run time, it calls the shell built in by default.
The shell built in time has limited options.
Hence, we should use an absolute path for the executable,
/user/bin/time, for performing additional functionalities.
We can write these time statistics to a file
using the -o filename option as follows.
The filename should always appear after the -o flag.
In order to append the time statistics to a file
without overwriting, use the -a flag
along with the -o option as follows.
We can also format the time outputs using the format strings
with -f option.
A format string consists of parameters corresponding
to specific options pre-fixed with percent.
Format strings for real time, user time and sys time
are as follows.
Real time, %E, user, %U, sys, %S.
By combining parameter strings,
we can create a formatted output as follows.
Here, %U, is the parameter for user time.
When a formatted output is produced,
the formatted output of the command
is written to the standard output
and the output of the command which is timed
is written to standard error.
We can redirect the format output
using the redirection operator, greater than,
and redirect the time information output
using the two greater than error redirection operator.
For example.
To show the page size, use the %Z parameter as follows.
Here, the output of the time command is not required
and hence the standard output is directed
to the /dev/null device, in order to prevent it
from writing to the terminal.
More format string parameters are available.
Try mantime for more details.
The three different times can be defined as follows,
real is wall clock time,
the time from start to finish
of the call.
This is all elapsed time including time slices
used by other processes,
and the time that the process spends when blocked,
for example, if it's waiting for I/O to complete.
User is the amount of CPU time spent in user mode code
outside the kernel within the process.
This is only the actual CPU time used
in executing the process.
Other processes and the time that the process
spends when blocked do not count towards this figure.
Sys is the amount of CPU time spent in the kernel
within the process.
This means executing the CPU time spent assistant calls
within the kernel, as opposed to the library code,
which is still running in the user space.
Like user time, this is only the CPU time
used by the process.
Refer to the following table for a brief description of
kernel mode, also known as supervisor mode,
an assistant core mechanism.
Many details regarding a process
can be collected using the time command.
The important details include, exit status,
number of signals received,
number of context switches made, and so on.
Each parameter can be displayed
by using a suitable format string.
The following points show some of the interesting parameters
that can be used.
%C, name, and command line arguments
of the command being timed.
%D, average size of the processes on shared data area
in kilobytes.
%E, elapsed, real, wall clock time used by the process
in hours, minutes, and seconds.
%X, exit stages of the command.
%K, number of signals delivered through the process.
%W, number of times the process was swapped out
of the main memory.
%Z, systems page size in bytes.
This is a per-system constant, but varies between systems.
%P, percentage of the CPU that this job got.
This is just the user plus system times
divided by the total running time.
It also prints a percentage sign.
%K, average total data, plus stack, plus text memory
used to drive the process in kilobytes.
%W, number of times that the program was context switched
voluntarily, for instance, when waiting for an I/O operation
to complete.
%C, number of times the process was context switched
involuntarily, because the time slice expired.
Awesome, we've successfully learned how to calculate
the execution time for a command.
In the next video, we'll learn about collecting information
about logged-in users, boot logs, and boot failures.