You may sometimes want to to find the total execution time of a command or program you run. To optimize a script or program, it’s essential to know how long does it take for it to complete execution. This helps you compare the total run time of Program A vs. Program B and optimize your code, especially if you’re a professional software developer, analyst, or software tester.
Measuring the total execution time of a script or process is one of the most critical aspects of performance optimization. This article discusses various methods to find the total execution time of a program, script, or command in Windows.
Find the Total Execution Time of a Program or Command
Using PowerShell
PowerShell includes the built-in Measure-Command
cmdlet that helps you measure the time it takes to run script blocks, cmdlets, or even external programs. Here is a sample PowerShell command-line which measures the times takes for the ping google.com
command-line to finish:
Measure-Command { ping google.com | Out-Host }
After the ping command’s output, you’ll see the following statistics at the bottom.
Days : 0 Hours : 0 Minutes : 0 Seconds : 3 Milliseconds : 66 Ticks : 30660017 TotalDays : 3.5486130787037E-05 TotalHours : 0.000851667138888889 TotalMinutes : 0.0511000283333333 TotalSeconds : 3.0660017 TotalMilliseconds : 3066.0017
Read more about Measure-Object at Microsoft Docs.
The command took 3.06 seconds to complete. The Out-Host
argument, if omitted, PowerShell hides the output (of Ping
command) in the console — you’ll see only the execution statistics.
If you have a lengthy command-line or you need to run multiple commands, you can put them in a Windows batch file and then run it using PowerShell. Here is an example:
Measure-Command { Start-Process "d:\batch\backup.bat" | Out-Host }
Using VBScript
Here is a simple VBScript that calculates the total execution time of a command-line, program, or batch file.
Set WshShell = WScript.CreateObject("WScript.Shell") sCmd = chr(34) & "D:\Batch Files\Backup.bat" & chr(34) dtmStartTime = Timer Return = WshShell.Run(sCmd, 1, true) Wscript.Echo "The task completed in " & Round(Timer - dtmStartTime, 2) & " seconds."
- Copy the above code to Notepad
- Save the file as
exec_timer.vbs
. - In the 2nd line that starts with
sCmd
, modify the program you want to run — i.e., replace"D:\Batch Files\Backup.bat"
in the above example with the application or batch you want to run. - Save the file and close Notepad.
- Double-click the VBScript file to run it.
The VBScript launches the batch file, command, or the program. After it finishes running, the total execution time is shown (in seconds.)
Using TimeIt.exe from Resource Kit Tools
TimeIt.exe
is part of the Windows Server 2003 Resource Kit (mirror link) tool that shows the program or command execution timings. This program runs fine on Windows 10 as well.
File info: rktools.exe - SHA256 - 6260d75a2cb30201c5424defec3ba505edb92b91802825e2fd6eb9bd58ed5cca
- After downloading the RK Tools, open rktools.exe using 7-Zip.
- Then, open inside
rktools.msi
- Find
TimeIt.exe
and extract to a folder.
To run a command or batch file and measure its total execution time, here are some examples:
timeit.exe ping google.com
timeit.exe C:\Batch Files\Measure command run time\backup.bat
TimeIt showed the process exit time and elapsed time, among other details. When I checked, the bytes written value didn’t match with the output file size. But, the bytes read/written info is not the subject of discussion in this article.
More Information
When writing the article about extracting audio from a video file using the FFmpeg.exe, I thought of measuring the execution time of the command. I used the above script to measure the time taken to extract MP3 audio from a movie file using the FFmpeg.exe command-line tool.
The 1st batch included this command-line:
ffmpeg -i "C:\Movies\Movie.mp4" -ss 01:20:00 -t 00:00:20 -codec:a libmp3lame -ab 128000 "D:\output-1.mp3"
The 2nd batch included this command-line:
ffmpeg -ss 01:20:00 -i "C:\Movies\Movie.mp4" -t 00:00:20 -codec:a libmp3lame -ab 128000 "D:\output-2.mp3"
As you can see, the number of command-line arguments and the respective values are exactly the same. The only difference is that the -ss
(starting point) parameter is placed before the -i
(file input) parameter, in the 2nd batch file. The method is called Input seeking as per FFmpeg.exe documentation. The 1st method is called Output seeking, wherein the -i
(input file) parameter appears before the -ss
parameter.
- FFmpeg documentation says that the input seeking method is much faster than output seeking, because when using the former method, FFmpeg.exe jumps (seeks) directly to the mentioned Start point in the input file and starts decoding from that point onwards.
- Whereas, the output seeking method opens the file first, starts decoding from
00:00:00
, and then discards the data up until it reaches the-ss
Start point01:20:00
. So, the system resources (CPU, Memory, and Disk I/O) are wasted as well as the execution time is increased when you use the Output seeking method.
The FFmpeg.exe documentation is correct. When I tested, the input seek ran much faster than the output seek method.
- The 1st command (Output seek) took 20.48 seconds to complete.
- The 2nd command (Input seek) took 16.62 seconds to complete.
If you know any other methods to measure the execution time of a process, script, or command, let’s know.
One small request: If you liked this post, please share this?
One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:- Pin it!
- Share it to your favorite blog + Facebook, Reddit
- Tweet it!
Problem with your PowerShell version.
If you need to run a batch file the command needs to be:
Measure-Command { “d:\batch\backup.bat” | Out-Host }
If you have Start-Process it launches the batch file in a new window and returns immediately thus not measuring the amount of time correctly.