Windows命令行中是否有内置的方法来测量命令的执行时间?
Windows命令行中是否有内置的方法来测量命令的执行时间?
我正在使用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
@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.
还有一个TimeMem(2012年3月):
这是一个Windows实用程序,执行程序并显示其执行时间、内存使用情况和IO统计信息。它的功能类似于Unix中的time实用程序。
根据您使用的Windows版本,仅运行bash
将使您进入Bash模式。这将允许您使用一些在PowerShell直接不可用的命令(如time
命令)。现在,计时您的命令就像执行以下命令一样简单:
# The clause <your-command> (without the angle brackets) denotes the command you want to run.
$ time <your-command>
注意:您可以在Bash模式下运行
exit
命令,轻松退出并返回到主流shell。
我尝试了其他方法(如Measure-Command
),有时会产生不需要的统计信息,在Windows 10中这对我非常有效。希望这对您也有效。
以下是Postfix计时器版本:
使用示例:
timeout 1 | TimeIt.cmd
Execution took ~969 milliseconds.
将以下内容复制并粘贴到某个编辑器中,例如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
^ - 基于'Daniel Sparks'版本
与measure-time相比,"Get-Date"是一种替代方法。使用它不需要处理输出等繁琐步骤。
$start = Get-Date
[System.Threading.Thread]::Sleep(1500)
$(Get-Date) - $start
输出:
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
$start = Get-Date ; "在此处输入慢速命令" ; ($(Get-Date) - $start)
- MarkHu这是一种避免延迟扩展的单行代码,它可能会干扰某些命令:
cmd /E /C "prompt $T$$ & echo.%TIME%$ & COMMAND_TO_MEASURE & for %Z in (.) do rem/ "
14:30:27.58$ ... 14:32:43.17$ rem/
对于长期测试,请将$T
替换为$D,$T
,将%TIME%
替换为%DATE%,%TIME%
以包括日期。
要在批处理文件中使用,请将%Z
替换为%%Z
。
这是一个改进的单行代码(不需要延迟扩展):
cmd /E /C "prompt $D, $T$$ & (for %# in (.) do rem/ ) & COMMAND_TO_MEASURE & for %# in (.) do prompt"
2015/09/01, 14:30:27.58$ rem/ ... 2015/09/01, 14:32:43.17$ prompt
这种方法不包括在结果中实例化新的cmd
进程,也不包括prompt
命令。
这里是我的方法,不需要转换和微软的工具。它可以帮助您确定编码持续时间(尽管仅限于24小时):
@echo off
:start
REM Start time storage
set ST=%time%
echo Process started at %ST%
echo.
echo.
REM Your commands
REM Your commands
REM Your commands
:end
REM Start Time Definition
for /f "tokens=1-3 delims=:" %%a in ("%ST%") do set /a h1=%%a & set /a m1=%%b & set /a s1=%%c
REM End Time Definition
for /f "tokens=1-3 delims=:" %%a in ("%TIME%") do set /a h2=%%a & set /a m2=%%b & set /a s2=%%c
REM Difference
set /a h3=%h2%-%h1% & set /a m3=%m2%-%m1% & set /a s3=%s2%-%s1%
REM Time Adjustment
if %h3% LSS 0 set /a h3=%h3%+24
if %m3% LSS 0 set /a m3=%m3%+60 & set /a h3=%h3%-1
if %s3% LSS 0 set /a s3=%s3%+60 & set /a m3=%m3%-1
echo Start : %ST%
echo End : %time%
echo.
echo Total : %h3%:%m3%:%s3%
echo.
pause
如果其他人也在寻找这个问题的答案,可以使用一个名为GetProcessTimes()
的Windows API函数。编写一个小的C程序来启动命令,进行此调用并返回进程时间似乎不需要太多工作。
我的代码可以给你提供运行时间,单位为毫秒,最长可以达到24小时。它是不受地区影响的,并且考虑了负值情况(如果代码在午夜经过,则会计算负值)。它使用了延迟扩展,并应该保存在cmd / bat文件中。
在您的代码之前:
SETLOCAL EnableDelayedExpansion
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set t=%%I
set /a t1 = %t:~8,1%*36000 + %t:~9,1%*3600 + %t:~10,1%*600 + %t:~11,1%*60 + %t:~12,1%*10 + %t:~13,1% && set t1=!t1!%t:~15,3%
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set t=%%I
set /a t2 = %t:~8,1%*36000 + %t:~9,1%*3600 + %t:~10,1%*600 + %t:~11,1%*60 + %t:~12,1%*10 + %t:~13,1% && set t2=!t2!%t:~15,3%
set /a t2-=t1 && if !t2! lss 0 set /a t2+=24*3600000
set /a "h=t2/3600000,t2%%=3600000,m=t2/60000,t2%%=60000" && set t2=00000!t2!&& set t2=!t2:~-5!
if %h% leq 9 (set h=0%h%) && if %m% leq 9 (set m=0%m%)
set t2=%h%:%m%:%t2:~0,2%.%t2:~2,3%
ENDLOCAL
变量t2
存储您的运行时间,您可以使用echo %t2%
来显示它。