Site icon Winhelponline

Find the Total Execution Time of a Command or Program in Windows

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 command-line to finish:

Measure-Command { ping | 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."
  1. Copy the above code to Notepad
  2. Save the file as exec_timer.vbs.
  3. 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.
  4. Save the file and close Notepad.
  5. 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
  1. After downloading the RK Tools, open rktools.exe using 7-Zip.
  2. Then, open inside rktools.msi
  3. 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
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.

The FFmpeg.exe documentation is correct. When I tested, the input seek ran much faster than the output seek method.

If you know any other methods to measure the execution time of a process, script, or command, let’s know.

Exit mobile version