Duration is the amount of time it takes for a process to complete. To use a real-world example: if you look at a clock and make note of the time, then go out for a walk, then return and see that fifteen minutes have elapsed, the duration of your walk was fifteen minutes.
How computers measure time
In modern computers, time can be measured by several different clocks.
- The real-time clock (RTC) uses a quartz crystal which vibrates at a constant frequency to keep track of "human time": years, months, days, hours, minutes, and seconds. However, its frequency (32768 Hz) is too slow to measure fractions of time smaller than 1/32768 of a second. Modern CPUs can complete an operation much faster than this, so the RTC cannot accurately measure the duration of a computer process.
- A programmable interval timer (PIT) is a hardware counter which can trigger an interrupt when a certain count total is reached. A common PIT is the Intel 8253 integrated circuit, originally designed to be used with the Intel 8080 processor. It operates at a frequency of 1.193182 MHz. It is used as the system clock by many operating systems, including Windows and Linux.
- A time stamp counter (TSC) is a 64-bit register located on all modern x86 CPUs. It counts individual CPU clock cycles, and was first introduced with the Pentium line of Intel CPUs. At first, it was useful in providing a clock that could measure smaller fractions of time than a RTC or PIT. However, it requires that the CPU operate at a constant speed to obtain an accurate measurement. Newer CPUs can change frequency (such as entering a "turbo" mode to gain temporary speed boosts, or throttling down to a lower speed when idle to conserve power), so the TSC, while still useful for other purposes, is no longer reliable for measuring constant time.
- The High Precision Event Timer (HPET) is a hardware timer, developed jointly by Intel and Microsoft, which is available on computers manufactured after 2005. In the CPU chipset on the motherboard, the HPET is usually located on the southbridge. It counts time in 64 bits at a minimum of 10 MHz, and employs three "comparator" counters (either 32-bit or 64-bit) to arrive at a highly precise time measurement. The HPET is used for many purposes in your computer, such as synchronizing the audio stream when playing a video.
The most accurate of these timing methods is the HPET.
How to time a process on your computer
There are various ways to find out how long a process takes to complete on your computer.
In Windows Command Prompt
In the Windows Command Prompt, there is no built-in way to time a command. However, there is at least one simple way to find out when your command started and stopped. You can create a batch file that uses the %time% environment variable. For instance, if you create a batch file called mytime.bat, and give it these lines:
@echo off set timestart=%time% %* echo Start : %timestart% echo Finish: %time%
The variable %time% is the current time, and %* is everything after the command name. So to time the dir command, for instance:
mytime dir c:\
Volume in drive C is Windows10 Volume Serial Number is 5CB6-BCD6 Directory of c:\ [directory listing...] 28 File(s) 4,500,506 bytes 12 Dir(s) 23,807,946,752 bytes free Start : 19:31:18.87 Finish: 19:31:18.89
Note: This method is not precise. It does not measure CPU time, only "wall clock" time. So, other programs running on your computer will affect the times shown. Also, the duration must be calculated manually, by subtracting the "start" time from the "finish" time.
In Windows PowerShell
The command will run, but any output will not be shown. Instead, the output will display information about how long it took for command to complete. For example:
Days : 0 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 14 Ticks : 142200 TotalDays : 1.64583333333333E-07 TotalHours : 3.95E-06 TotalMinutes : 0.000237 TotalSeconds : 0.01422 TotalMilliseconds : 14.22
In Linux, you can prefix any shell command with the time command to measure its duration. For instance, the following command will find any files in or beneath the current directory which contain the word "hope", and report how long it took to find them:
time find /home/myuser -iname '*hope*'
/home/myuser/files/computer-hope.jpg /home/myuser/backup/hope-backup.zip real 0m0.298s user 0m0.164s sys 0m0.088s
Unlike Measure-Command, the Linux time displays all output from the command being timed. Then, it reports three times: user time (how much time the command actually spent running), sys time (how much time was spent by the kernel performing required system tasks), and real time (total time elapsed).