Windows命令行中是否有内置的方法来测量命令的执行时间?
Windows命令行中是否有内置的方法来测量命令的执行时间?
PowerShell有一个名为Measure-Command
的命令,可以用来进行此操作。您需要确保运行该命令的计算机上安装了PowerShell。
PS> Measure-Command { echo hi }
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 0
Ticks : 1318
TotalDays : 1.52546296296296E-09
TotalHours : 3.66111111111111E-08
TotalMinutes : 2.19666666666667E-06
TotalSeconds : 0.0001318
TotalMilliseconds : 0.1318
Measure-Command
捕获命令的输出。您可以使用 Out-Default
将输出重定向回控制台:
PS> Measure-Command { echo hi | Out-Default }
hi
Days : 0
...
正如Makotoe所评论的那样,Measure-Command
返回一个TimeSpan
对象,因此测量时间被打印为一堆字段。您可以使用ToString()
将对象格式化为时间戳字符串:
PS> (Measure-Command { echo hi | Out-Default }).ToString()
hi
00:00:00.0001318
如果在 Measure-Command
中的命令改变了你的控制台文本颜色,使用 [Console]::ResetColor()
来将它重置回正常。
如果你想:
尝试将以下脚本复制到新的批处理文件中(例如timecmd.bat):
@echo off
@setlocal
set start=%time%
:: Runs your command
cmd /c %*
set end=%time%
set options="tokens=1-4 delims=:.,"
for /f %options% %%a in ("%start%") do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
for /f %options% %%a in ("%end%") do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 100
set /a hours=%end_h%-%start_h%
set /a mins=%end_m%-%start_m%
set /a secs=%end_s%-%start_s%
set /a ms=%end_ms%-%start_ms%
if %ms% lss 0 set /a secs = %secs% - 1 & set /a ms = 100%ms%
if %secs% lss 0 set /a mins = %mins% - 1 & set /a secs = 60%secs%
if %mins% lss 0 set /a hours = %hours% - 1 & set /a mins = 60%mins%
if %hours% lss 0 set /a hours = 24%hours%
if 1%ms% lss 100 set ms=0%ms%
:: Mission accomplished
set /a totalsecs = %hours%*3600 + %mins%*60 + %secs%
echo command took %hours%:%mins%:%secs%.%ms% (%totalsecs%.%ms%s total)
使用方法
如果您将timecmd.bat放置在路径中的某个目录中,则可以从任何地方像这样调用它:
timecmd [your command]
例如
C:\>timecmd pause
Press any key to continue . . .
command took 0:0:1.18
timecmd "dir c:\windows /s > nul"
这应该处理从午夜之前到午夜之后运行的命令,但如果您的命令运行了24小时或更长时间,则输出将不正确。
timecmd pause
命令时,它总是显示 1.00 秒、2.00 秒、4.00 秒……甚至是 0.00 秒!操作系统为 Windows 7。 - Camilo Martinset start=10:10:10.10
set end=10:11:10.09
它将输出:
command took 0:1:-1.99 (59.99s total)
您应该颠倒执行“lss 0”比较的4行代码的顺序,以便进位从低到高的六十进制数字正确进行。
然后输出变为:
command took 0:0:59.99 (59.99s total)
- Jean-François Larvoire呵呵,最简单的解决方案可能是这样的:
echo %time%
YourApp.exe
echo %time%
这在所有的Windows系统上都可以直接使用。
如果一个应用程序使用控制台输出,那么将开始时间存储在临时变量中可能会更方便:
set startTime=%time%
YourApp.exe
echo Start Time: %startTime%
echo Finish Time: %time%
set startTime=%time% \n YourApp.exe \n echo 开始时间:%startTime% \n echo 完成时间:%time%
- Jonoecho %time% && YourCmd --with -- -params && echo %time%
- DaJunkie这是对Casey.K的回答的一点扩展,关于如何使用Measure-Command
工具来测试PowerShell脚本的性能:
You can invoke PowerShell from the standard command prompt, like this:
powershell -Command "Measure-Command {echo hi}"
This will eat the standard output, but you can prevent that by adding | Out-Default
like this from PowerShell:
Measure-Command {echo hi | Out-Default}
Or from a command prompt:
powershell -Command "Measure-Command {echo hi | Out-Default}"
*.ps1
或*.bat
中。measureTime.bat "c:\program files\some dir\program.exe"
。 - GetFree如果你正在使用Windows 2003(请注意,不支持Windows Server 2008及更高版本),你可以使用包含timeit.exe的Windows Server 2003资源工具包,它会显示详细的执行统计信息。以下是一个示例,用于计时命令"timeit -?":
C:\>timeit timeit -?
Invalid switch -?
Usage: TIMEIT [-f filename] [-a] [-c] [-i] [-d] [-s] [-t] [-k keyname | -r keyname] [-m mask] [commandline...]
where: -f specifies the name of the database file where TIMEIT
keeps a history of previous timings. Default is .\timeit.dat
-k specifies the keyname to use for this timing run
-r specifies the keyname to remove from the database. If
keyname is followed by a comma and a number then it will
remove the slowest (positive number) or fastest (negative)
times for that keyname.
-a specifies that timeit should display average of all timings
for the specified key.
-i specifies to ignore non-zero return codes from program
-d specifies to show detail for average
-s specifies to suppress system wide counters
-t specifies to tabular output
-c specifies to force a resort of the data base
-m specifies the processor affinity mask
Version Number: Windows NT 5.2 (Build 3790)
Exit Time: 7:38 am, Wednesday, April 15 2009
Elapsed Time: 0:00:00.000
Process Time: 0:00:00.015
System Calls: 731
Context Switches: 299
Page Faults: 515
Bytes Read: 0
Bytes Written: 0
Bytes Other: 298
您可以在Windows 2003资源工具包中获取TimeIt。它无法直接从Microsoft下载中心下载,但仍可以从archive.org - Windows Server 2003 Resource Kit Tools获取。
我在Windows Server 2008 R2上使用的一行命令是:
cmd /v:on /c "echo !TIME! & *mycommand* & echo !TIME!"
只要mycommand不需要引号(这会干扰cmd的引号处理),/v:on
是为了允许两个不同的TIME值独立评估,而不是在执行命令时只评估一次。^
(例如^"
)来转义引号。 - n611x007cmd /v:on /c echo !TIME! & echo "Quoted mycommand" & cmd /v:on /c echo !TIME!
。 - Maarten Penningscall
和 ^
。那些方法不起作用。我的意思是,我得到了相同的结果。这个方法和它们之间有什么区别? - akinuri。
prompt $d $t $_$P$G
它会给你类似于:
2009年3月23日15:45:50.77
C:\>
如果您有一个执行命令的小批处理脚本,请在每个命令前空一行,例如:
(空行)
myCommand.exe
(下一个空行)
myCommand2.exe
您可以通过提示中的时间信息计算每个命令的执行时间。最好的方法可能是将输出导入到文本文件进行进一步分析:
MyBatchFile.bat > output.txt
CMD
脚本中实现此功能并测量该脚本持续时间的示例,最好是在脚本内部实现。 - Jon Grah@echo off
time < nul
yourexecutable.exe > c:\temp\output.txt
time < nul
rem on newer windows system you can try time /T
The current time is: 10:31:57.92
Enter the new time:
The current time is: 10:32:05.94
Enter the new time:
time
命令有一个 /t
选项,它只显示当前系统时间,而不提示您输入新时间。但是,当您这样做时,它只显示小时和分钟,这可能对某些用途不够好。 (请参见 John Snow 对此问题的回答。) - martineauecho %time%
,它提供与 time < nul
相同的格式和精度,但避免了 "输入新时间:" 的输出。 - aschipfl我使用名为“GS Timer”的免费软件。
只需制作像这样的批处理文件:
timer
yourapp.exe
timer /s
如果您需要一组时间,请将timer /s的输出导入到一个.txt文件中。
您可以在此处获取:Gammadyne's免费DOS实用程序
分辨率为0.1秒。
.exe
进行基准测试,请使用以下命令:Measure-Command { .\your.exe }
。显然,PowerShell 除非明确告知,否则不会从当前目录运行程序。 - Cristian Diaconescupowershell -Command Measure-Command { YOUR_COMMAND }
дёЌдјљйљђи—Џstdout
。它可以测量运行YOUR_COMMAND
ж‰ЂйњЂзљ„ж—¶й—ґгЂ‚ - Behrouz.M