Kur*_*aze 463 windows time cmd batch-file windows-server-2003
是否有内置方法来测量Windows命令行上命令的执行时间?
小智 442
或者,Windows PowerShell具有类似于Bash的"time"命令的内置命令.它被称为"测量 - 命令".您必须确保在运行它的计算机上安装PowerShell.
示例输入:
Measure-Command {echo hi}
Run Code Online (Sandbox Code Playgroud)
示例输出:
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
Run Code Online (Sandbox Code Playgroud)
Luk*_*son 271
如果你想
尝试将以下脚本复制到新的批处理文件中(例如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)
Run Code Online (Sandbox Code Playgroud)
用法
如果将timecmd.bat放在路径中的目录中,可以从以下任何位置调用它:
timecmd [your command]
Run Code Online (Sandbox Code Playgroud)
例如
C:\>timecmd pause
Press any key to continue . . .
command took 0:0:1.18
Run Code Online (Sandbox Code Playgroud)
如果要进行输出重定向,可以像这样引用命令:
timecmd "dir c:\windows /s > nul"
Run Code Online (Sandbox Code Playgroud)
这应该处理从午前到午夜之间运行的命令,但如果命令运行24小时或更长时间,输出将是错误的.
Lie*_*nes 219
呵呵,最简单的解决方案可能就是:
echo %time%
YourApp.exe
echo %time%
Run Code Online (Sandbox Code Playgroud)
这适用于每个开箱即用的Windows.
如果应用程序使用控制台输出,则可以方便地将开始时间存储在临时变量中:
set startTime=%time%
YourApp.exe
echo Start Time: %startTime%
echo Finish Time: %time%
Run Code Online (Sandbox Code Playgroud)
小智 109
如果您使用的是Windows 2003(请注意,不支持Windows Server 2008及更高版本),您可以使用Windows Server 2003资源工具包,其中包含显示详细执行统计信息的timeit.exe.这是一个例子,计时命令"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
Run Code Online (Sandbox Code Playgroud)
您可以在Windows 2003 Resource Kit中获取TimeIt.在这里下载 .
小智 88
只是一点点扩大,从Casey.K回答有关使用Measure-Command
从PowerShell的:
您可以从标准命令提示符调用PowerShell,如下所示:
powershell -Command "Measure-Command {echo hi}"
Run Code Online (Sandbox Code Playgroud)这将使用标准输出,但您可以通过| Out-Default
从PowerShell 添加这样来阻止它:
Measure-Command {echo hi | Out-Default}
Run Code Online (Sandbox Code Playgroud)
或者从命令提示符:
powershell -Command "Measure-Command {echo hi | Out-Default}"
Run Code Online (Sandbox Code Playgroud)当然,你可以自由在脚本文件来包装这个*.ps1
或*.bat
.
Nat*_*ing 51
我在Windows Server 2008 R2中使用的单行程序是:
cmd /v:on /c "echo !TIME! & *mycommand* & echo !TIME!"
Run Code Online (Sandbox Code Playgroud)
只要mycommand不需要引号(使用cmd的引用处理).的/v:on
是,以允许与在命令的执行进行评估独立地,而不是一旦两个不同的时间值.
Tre*_*reb 47
如果打开命令窗口并手动调用命令,则可以在每个提示上显示时间戳,例如
prompt $d $t $_$P$G
Run Code Online (Sandbox Code Playgroud)
它给你的东西:
23.03.2009 15:45:50,77
C:\>
如果您有一个执行命令的小批处理脚本,则在每个命令之前都有一个空行,例如
(空行)
myCommand.exe
(下一个空行)
myCommand2.exe
您可以通过提示中的时间信息计算每个命令的执行时间.最好的方法是将输出传递给文本文件以进行进一步分析:
MyBatchFile.bat > output.txt
Run Code Online (Sandbox Code Playgroud)
Abe*_*ker 25
由于其他人建议安装免费软件和PowerShell之类的东西,你也可以安装Cygwin,它可以让你访问许多基本的Unix命令,比如时间:
abe@abe-PC:~$ time sleep 5
real 0m5.012s
user 0m0.000s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
不确定Cygwin增加了多少开销.
Joh*_*hnW 22
不像Unix上的某些功能那么优雅,但创建一个cmd文件,如下所示:
@echo off
time < nul
yourexecutable.exe > c:\temp\output.txt
time < nul
rem on newer windows system you can try time /T
Run Code Online (Sandbox Code Playgroud)
这将显示开始和停止时间,如下所示:
The current time is: 10:31:57.92
Enter the new time:
The current time is: 10:32:05.94
Enter the new time:
Run Code Online (Sandbox Code Playgroud)
pep*_*uan 17
我使用名为"GS Timer"的免费软件.
只需制作一个这样的批处理文件:
timer
yourapp.exe
timer /s
Run Code Online (Sandbox Code Playgroud)
如果需要一组时间,只需将timer/s的输出传递到.txt文件中.
你可以在这里得到它:Gammadyne的免费DOS工具
分辨率为0.1秒.
小智 14
我正在使用Windows XP,由于某种原因,timeit.exe对我不起作用.我找到了另一种选择 - PTIME.这非常有效.
http://www.pc-tools.net/win32/ptime/
示例 -
C:\> ptime
ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>
Syntax: ptime command [arguments ...]
ptime will run the specified command and measure the execution time
(run time) in seconds, accurate to 5 millisecond or better. It is an
automatic process timer, or program timer.
C:\> ptime cd
ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>
=== cd ===
C:\
Execution time: 0.015 s
Run Code Online (Sandbox Code Playgroud)
小智 10
只要它持续时间不超过24小时......
@echo off
set starttime=%TIME%
set startcsec=%STARTTIME:~9,2%
set startsecs=%STARTTIME:~6,2%
set startmins=%STARTTIME:~3,2%
set starthour=%STARTTIME:~0,2%
set /a starttime=(%starthour%*60*60*100)+(%startmins%*60*100)+(%startsecs%*100)+(%startcsec%)
:TimeThis
ping localhost
set endtime=%time%
set endcsec=%endTIME:~9,2%
set endsecs=%endTIME:~6,2%
set endmins=%endTIME:~3,2%
set endhour=%endTIME:~0,2%
if %endhour% LSS %starthour% set /a endhour+=24
set /a endtime=(%endhour%*60*60*100)+(%endmins%*60*100)+(%endsecs%*100)+(%endcsec%)
set /a timetaken= ( %endtime% - %starttime% )
set /a timetakens= %timetaken% / 100
set timetaken=%timetakens%.%timetaken:~-2%
echo.
echo Took: %timetaken% sec.
Run Code Online (Sandbox Code Playgroud)
这里有一个
用法示例:
超时 1 | TimeIt.cmd
Execution took ~969 milliseconds.
Run Code Online (Sandbox Code Playgroud)
将其复制并粘贴到一些编辑器中,例如Notepad++并将其另存为TimeIt.cmd:
:: --- TimeIt.cmd ----
@echo off
setlocal enabledelayedexpansion
call :ShowHelp
:: Set pipeline initialization time
set t1=%time%
:: Wait for stdin
more
:: Set time at which stdin was ready
set t2=!time!
:: Calculate difference
Call :GetMSeconds Tms1 t1
Call :GetMSeconds Tms2 t2
set /a deltaMSecs=%Tms2%-%Tms1%
echo Execution took ~ %deltaMSecs% milliseconds.
endlocal
goto :eof
:GetMSeconds
Call :Parse TimeAsArgs %2
Call :CalcMSeconds %1 %TimeAsArgs%
goto :eof
:CalcMSeconds
set /a %1= (%2 * 3600*1000) + (%3 * 60*1000) + (%4 * 1000) + (%5)
goto :eof
:Parse
:: Mask time like " 0:23:29,12"
set %1=!%2: 0=0!
:: Replace time separators with " "
set %1=!%1::= !
set %1=!%1:.= !
set %1=!%1:,= !
:: Delete leading zero - so it'll not parsed as octal later
set %1=!%1: 0= !
goto :eof
:ShowHelp
echo %~n0 V1.0 [Dez 2015]
echo.
echo Usage: ^<Command^> ^| %~nx0
echo.
echo Wait for pipe getting ready... :)
echo (Press Ctrl+Z ^<Enter^> to Cancel)
goto :eof
Run Code Online (Sandbox Code Playgroud)
^ - 基于“丹尼尔斯帕克斯”版本
小智 7
根据您使用的 Windows 版本,只需运行即可bash
让您进入 Bash 模式。这将允许您直接使用 PowerShell 上不可用的一堆命令(如time
命令)。计时命令现在就像执行一样简单:
# The clause <your-command> (without the angle brackets) denotes the command you want to run.
$ time <your-command>
Run Code Online (Sandbox Code Playgroud)
注意:您可以通过
exit
在 Bash 模式下运行轻松退出 Bash 模式并返回到主流 shell 。
在尝试了Measure-Command
有时会产生不需要的统计数据的其他方法(如)后,这对我来说非常有效(Windows 10)。希望这也适用于您。
测量时间的替代方法就是“Get-Date”。您没有转发输出等麻烦。
$start = Get-Date
[System.Threading.Thread]::Sleep(1500)
$(Get-Date) - $start
Run Code Online (Sandbox Code Playgroud)
输出:
Days : 0
Hours : 0
Minutes : 0
Seconds : 1
Milliseconds : 506
Ticks : 15060003
TotalDays : 1.74305590277778E-05
TotalHours : 0.000418333416666667
TotalMinutes : 0.025100005
TotalSeconds : 1.5060003
TotalMilliseconds : 1506.0003
Run Code Online (Sandbox Code Playgroud)
这是一个避免延迟扩展的单行,这可能会干扰某些命令:
cmd /E /C "prompt $T$$ & echo.%TIME%$ & COMMAND_TO_MEASURE & for %Z in (.) do rem/ "
Run Code Online (Sandbox Code Playgroud)
输出类似于:
Run Code Online (Sandbox Code Playgroud)14:30:27.58$ ... 14:32:43.17$ rem/
对于长期测试,替换$T
by$D, $T
和%TIME%
by%DATE%, %TIME%
以包括日期。
要在批处理文件中使用它,请替换%Z
为%%Z
.
这是一个改进的单行(也没有延迟扩展):
cmd /E /C "prompt $D, $T$$ & (for %# in (.) do rem/ ) & COMMAND_TO_MEASURE & for %# in (.) do prompt"
Run Code Online (Sandbox Code Playgroud)
输出类似于以下内容:
Run Code Online (Sandbox Code Playgroud)2015/09/01, 14:30:27.58$ rem/ ... 2015/09/01, 14:32:43.17$ prompt
这种方法不包括cmd
在结果中实例化新的过程,也不包括prompt
命令。
如果安装了CMake,则只需运行以下命令即可。
cmake -E time <the_command_to_measure_run_time>
Run Code Online (Sandbox Code Playgroud)