[英]How do I measure execution time of a command on the Windows command line?
是否有內置方法來測量 Windows 命令行上的命令執行時間?
PowerShell 有一個名為Measure-Command
的 cmdlet。 您必須確保 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
捕獲命令的 output。 您可以使用Out-Default
將 output 重定向回您的控制台:
PS> Measure-Command { echo hi | Out-Default }
hi
Days : 0
...
正如Makotoe評論的那樣, Measure-Command
返回一個TimeSpan
object,因此測量的時間被打印為一堆字段。 您可以使用ToString()
將 object 格式化為時間戳字符串:
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
如果你想做 output 重定向,你可以這樣引用命令:
timecmd "dir c:\windows /s > nul"
這應該處理從午夜之前到午夜之后運行的命令,但是如果您的命令運行 24 小時或更長時間,output 將會出錯。
呵呵,最簡單的解決辦法可能是這樣的:
echo %time%
YourApp.exe
echo %time%
這適用於開箱即用的每個 Windows。
如果應用程序使用控制台 output,將開始時間存儲在臨時變量中可能會很方便:
set startTime=%time%
YourApp.exe
echo Start Time: %startTime%
echo Finish Time: %time%
只是對 Casey.K 關於使用PowerShell中的Measure-Command
的回答進行了一點擴展:
您可以從標准命令提示符調用 PowerShell,如下所示:
powershell -Command "Measure-Command {echo hi}"
這會吃掉標准的 output,但你可以通過添加| Out-Default
來自 PowerShell | Out-Default
如下:
Measure-Command {echo hi | Out-Default}
或者從命令提示符:
powershell -Command "Measure-Command {echo hi | Out-Default}"
當然,您可以自由地將其包裝在腳本文件*.ps1
或*.bat
中。
如果您使用的是 Windows 2003(注意不支持 windows server 2008 及更高版本),您可以使用 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 值,而不是在執行命令時評估一次。
如果您打開命令 window 並手動調用命令,則可以在每個提示符上顯示時間戳,例如
prompt $d $t $_$P$G
它給了你類似的東西:
23.03.2009 15:45:50,77
C:\>
如果您有一個執行命令的小批量腳本,請在每個命令之前留一個空行,例如
(空行)
我的命令
(下一個空行)
我的命令2.exe
您可以通過提示符中的時間信息來計算每個命令的執行時間。 最好的可能是將 pipe 和 output 轉換為文本文件以進行進一步分析:
MyBatchFile.bat > output.txt
不如 Unix 上的某些功能那么優雅,但創建一個 cmd 文件,如下所示:
@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:
我使用名為“GS Timer”的免費軟件。
只需像這樣制作一個批處理文件:
timer
yourapp.exe
timer /s
如果需要一組時間,只需將pipe、output的定時器/s寫入一個.txt文件即可。
你可以在這里得到它: Gammadyne 的免費 DOS 實用程序
分辨率為 0.1 秒。
我使用的是 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
只要不超過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.
還有TimeMem (2012 年 3 月):
這是一個 Windows 實用程序,它執行程序並顯示其執行時間、memory 使用情況和 IO 統計信息。 它的功能類似於 Unix 時間實用程序。
這里有一個
使用示例:
超時 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
^ - 基於“丹尼爾斯帕克斯”版本
根據您使用的 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
輕松退出 Bash 模式並返回主流 shell。
在嘗試了有時會產生不希望的統計數據的其他方法(如Measure-Command
)后,這對我來說非常有效(Windows 10)。 希望這也適用於你。
測量時間的替代方法是簡單的“獲取日期”。 轉發 output 等就沒有那么麻煩了。
$start = Get-Date
[System.Threading.Thread]::Sleep(1500)
$(Get-Date) - $start
Output:
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
這是一種避免延遲擴展的單行程序,這可能會干擾某些命令:
cmd /E /C "prompt $T$$ & echo.%TIME%$ & COMMAND_TO_MEASURE & for %Z in (.) do rem/ "
output 類似於:
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"
output 看起來與此類似:
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
我的代碼以毫秒為單位為您提供運行時間,最長為 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
如果您想要 HH:mm:ss.000 格式的運行時間,請添加:
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%
來顯示它。
如果其他人來這里尋找這個問題的答案,有一個 Windows API function 稱為GetProcessTimes()
。 編寫一個 C 小程序來啟動命令、進行此調用並返回處理時間看起來並不需要太多工作。
在您的程序所在的目錄中,鍵入notepad mytimer.bat
,單擊“是”以創建一個新文件。
粘貼下面的代碼,將YourApp.exe
替換為您的程序,然后保存。
@echo off date /t time /t YourApp.exe date /t time /t
在命令行中鍵入mytimer.bat
,然后按 Enter。
這是對 Luke Sampson 的漂亮timecmd.bat
的評論/編輯並回復
出於某種原因,這只在整秒鍾內給了我 output ......這對我來說沒用。 我的意思是我運行 timecmd pause,它總是導致 1.00 秒、2.00 秒、4.00 秒……甚至 0.00 秒。 Windows 7: – Camilo Martin 2013 年 9 月 25 日 16:00"
在某些配置中,分隔符可能不同。 以下更改應至少涵蓋大多數西方國家。
set options="tokens=1-4 delims=:,." (added comma)
添加 ',' 后, %time%
毫秒在我的系統上工作
(*因為站點不允許匿名評論,並且即使我總是使用相同的訪客 email 並結合 ipv6 ip 和瀏覽器指紋,也不能很好地跟蹤身份,應該足以在沒有密碼的情況下唯一識別)
@echo off & setlocal
set start=%time%
REM Do stuff to be timed here.
REM Alternatively, uncomment the line below to be able to
REM pass in the command to be timed when running this script.
REM cmd /c %*
set end=%time%
REM Calculate time taken in seconds, to the hundredth of a second.
REM Assumes start time and end time will be on the same day.
set options="tokens=1-4 delims=:."
for /f %options% %%a in ("%start%") do (
set /a start_s="(100%%a %% 100)*3600 + (100%%b %% 100)*60 + (100%%c %% 100)"
set /a start_hs=100%%d %% 100
)
for /f %options% %%a in ("%end%") do (
set /a end_s="(100%%a %% 100)*3600 + (100%%b %% 100)*60 + (100%%c %% 100)"
set /a end_hs=100%%d %% 100
)
set /a s=%end_s%-%start_s%
set /a hs=%end_hs%-%start_hs%
if %hs% lss 0 (
set /a s=%s%-1
set /a hs=100%hs%
)
if 1%hs% lss 100 set hs=0%hs%
echo.
echo Time taken: %s%.%hs% secs
echo.
以下腳本僅使用“cmd.exe”並輸出從創建管道到腳本之前的進程退出的毫秒數。 即,鍵入您的命令,然后 pipe 到腳本。 示例:“timeout 3 | runtime.cmd”應該產生類似“2990”的結果。 If you need both the runtime output and the stdin output, redirect stdin before the pipe - ex: "dir /s 1>temp.txt | runtime.cmd" would dump the output of the "dir" command to "temp.txt"並將運行時打印到控制台。
:: --- runtime.cmd ----
@echo off
setlocal enabledelayedexpansion
:: find target for recursive calls
if not "%1"=="" (
shift /1
goto :%1
exit /b
)
:: set pipeline initialization time
set t1=%time%
:: wait for stdin
more > nul
:: set time at which stdin was ready
set t2=!time!
::parse t1
set t1=!t1::= !
set t1=!t1:.= !
set t1=!t1: 0= !
:: parse t2
set t2=!t2::= !
set t2=!t2:.= !
set t2=!t2: 0= !
:: calc difference
pushd %~dp0
for /f %%i in ('%0 calc !t1!') do for /f %%j in ('%0 calc !t2!') do (
set /a t=%%j-%%i
echo !t!
)
popd
exit /b
goto :eof
:calc
set /a t=(%1*(3600*1000))+(%2*(60*1000))+(%3*1000)+(%4)
echo !t!
goto :eof
endlocal
driblio的答案可以做得更短一些(雖然可讀性不強)
@echo off
:: Calculate the start timestamp
set _time=%time%
set /a _hours=100%_time:~0,2%%%100,_min=100%_time:~3,2%%%100,_sec=100%_time:~6,2%%%100,_cs=%_time:~9,2%
set /a _started=_hours*60*60*100+_min*60*100+_sec*100+_cs
:: yourCommandHere
:: Calculate the difference in cSeconds
set _time=%time%
set /a _hours=100%_time:~0,2%%%100,_min=100%_time:~3,2%%%100,_sec=100%_time:~6,2%%%100,_cs=%_time:~9,2%
set /a _duration=_hours*60*60*100+_min*60*100+_sec*100+_cs-_started
:: Populate variables for rendering (100+ needed for padding)
set /a _hours=_duration/60/60/100,_min=100+_duration/60/100%%60,_sec=100+(_duration/100%%60%%60),_cs=100+_duration%%100
echo Done at: %_time% took : %_hours%:%_min:~-2%:%_sec:~-2%.%_cs:~-2%
::prints something like:
::Done at: 12:37:53,70 took: 0:02:03.55
根據 Luke Sampson 的說法,這個版本是八進制安全的,盡管任務應該在 24 小時內完成。
讓 Perl 安裝了可用的租用解決方案,運行:
C:\BATCH>time.pl "echo Fine result"
0.01063
Fine result
STDERR 出現在測量秒數之前
#!/usr/bin/perl -w
use Time::HiRes qw();
my $T0 = [ Time::HiRes::gettimeofday ];
my $stdout = `@ARGV`;
my $time_elapsed = Time::HiRes::tv_interval( $T0 );
print $time_elapsed, "\n";
print $stdout;
@echo off
setlocal enableextensions
REM set start time env var
FOR /F "tokens=* USEBACKQ" %%F IN (`php -r "echo microtime(true);"`) DO ( SET start_time=%%F )
## PUT_HERE_THE_COMMAND_TO_RUN ##
REM echo elapsed time
php -r "echo 'elapsed: ' . (round(microtime(true) - trim(getenv('start_time')), 2)) . ' seconds' . mb_convert_encoding(' ', 'UTF-8', 'HTML-ENTITIES');"
不需要 cygwin 或不受信任的實用程序。 PHP 本地可用時有用
精度和 output 格式可以輕松調整
同樣的想法可以移植到 PowerShell
powershell 的另一種方法:
@echo off
for /f %%t in ('powershell "(get-date).tofiletime()"') do set mst=%%t
rem some commands
powershell ((get-date).tofiletime() - %mst%)
這將以毫秒為單位打印執行時間。
以下腳本模擬 *nix 紀元時間,但它是本地和區域的。 它應該處理包括閏年在內的日歷邊緣情況。 如果Cygwin可用,則可以通過指定Cygwin選項來比較 epoch 值。
我在美國東部標准時間,報告的差異是 4 小時,這是相對正確的。 有一些有趣的解決方案可以刪除 TZ 和區域依賴項,但我沒有注意到任何微不足道的問題。
@ECHO off
SETLOCAL EnableDelayedExpansion
::
:: Emulates local epoch seconds
::
:: Call passing local date and time
CALL :SECONDS "%DATE%" "%TIME%"
IF !SECONDS! LEQ 0 GOTO END
:: Not testing - print and exit
IF NOT "%~1"=="cygwin" (
ECHO !SECONDS!
GOTO END
)
:: Call on Cygwin to get epoch time
FOR /F %%c IN ('C:\cygwin\bin\date +%%s') DO SET EPOCH=%%c
:: Show the results
ECHO Local Seconds: !SECONDS!
ECHO Epoch Seconds: !EPOCH!
:: Calculate difference between script and Cygwin
SET /A HOURS=(!EPOCH!-!SECONDS!)/3600
SET /A FRAC=(!EPOCH!-!SECONDS!)%%3600
:: Delta hours shown reflect TZ
ECHO Delta Hours: !HOURS! Remainder: !FRAC!
GOTO END
:SECONDS
SETLOCAL EnableDelayedExpansion
:: Expecting values from caller
SET DATE=%~1
SET TIME=%~2
:: Emulate Unix epoch time without considering TZ
SET "SINCE_YEAR=1970"
:: Regional constraint! Expecting date and time in the following formats:
:: Sun 03/08/2015 Day MM/DD/YYYY
:: 20:04:53.64 HH:MM:SS
SET VALID_DATE=0
ECHO !DATE! | FINDSTR /R /C:"^... [0-9 ][0-9]/[0-9 ][0-9]/[0-9][0-9][0-9][0-9]" > nul && SET VALID_DATE=1
SET VALID_TIME=0
ECHO !TIME! | FINDSTR /R /C:"^[0-9 ][0-9]:[0-9 ][0-9]:[0-9 ][0-9]" > nul && SET VALID_TIME=1
IF NOT "!VALID_DATE!!VALID_TIME!"=="11" (
IF !VALID_DATE! EQU 0 ECHO Unsupported Date value: !DATE! 1>&2
IF !VALID_TIME! EQU 0 ECHO Unsupported Time value: !TIME! 1>&2
SET SECONDS=0
GOTO SECONDS_END
)
:: Parse values
SET "YYYY=!DATE:~10,4!"
SET "MM=!DATE:~4,2!"
SET "DD=!DATE:~7,2!"
SET "HH=!TIME:~0,2!"
SET "NN=!TIME:~3,2!"
SET "SS=!TIME:~6,2!"
SET /A YEARS=!YYYY!-!SINCE_YEAR!
SET /A DAYS=!YEARS!*365
:: Bump year if after February - want leading zeroes for this test
IF "!MM!!DD!" GEQ "0301" SET /A YEARS+=1
:: Remove leading zeros that can cause octet probs for SET /A
FOR %%r IN (MM,DD,HH,NN,SS) DO (
SET "v=%%r"
SET "t=!%%r!"
SET /A N=!t:~0,1!0
IF 0 EQU !N! SET "!v!=!t:~1!"
)
:: Increase days according to number of leap years
SET /A DAYS+=(!YEARS!+3)/4-(!SINCE_YEAR!%%4+3)/4
:: Increase days by preceding months of current year
FOR %%n IN (31:1,28:2,31:3,30:4,31:5,30:6,31:7,31:8,30:9,31:10,30:11) DO (
SET "n=%%n"
IF !MM! GTR !n:~3! SET /A DAYS+=!n:~0,2!
)
:: Multiply and add it all together
SET /A SECONDS=(!DAYS!+!DD!-1)*86400+!HH!*3600+!NN!*60+!SS!
:SECONDS_END
ENDLOCAL & SET "SECONDS=%SECONDS%"
GOTO :EOF
:END
ENDLOCAL
使用 sub 以百分之一秒返回時間
::tiemeit.cmd
@echo off
Setlocal EnableDelayedExpansion
call :clock
::call your_command or more > null to pipe this batch after your_command
call :clock
echo %timed%
pause
goto:eof
:clock
if not defined timed set timed=0
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
set /A timed = "(((1%%a - 100) * 60 + (1%%b - 100)) * 60 + (1%%c - 100)) * 100 + (1%%d - 100)- %timed%"
)
goto:eof
具有區域格式、24 小時和混合輸入支持的“精益和平均”計時器
調整Aacini 的替換方法體,沒有 IF,只有一個 FOR(我的區域修復)
1:文件timer.bat放置在 %PATH% 或當前目錄中的某處
@echo off & rem :AveYo: compact timer function with Regional format, 24-hours and mixed input support
if not defined timer_set (if not "%~1"=="" (call set "timer_set=%~1") else set "timer_set=%TIME: =0%") & goto :eof
(if not "%~1"=="" (call set "timer_end=%~1") else set "timer_end=%TIME: =0%") & setlocal EnableDelayedExpansion
for /f "tokens=1-6 delims=0123456789" %%i in ("%timer_end%%timer_set%") do (set CE=%%i&set DE=%%k&set CS=%%l&set DS=%%n)
set "TE=!timer_end:%DE%=%%100)*100+1!" & set "TS=!timer_set:%DS%=%%100)*100+1!"
set/A "T=((((10!TE:%CE%=%%100)*60+1!%%100)-((((10!TS:%CS%=%%100)*60+1!%%100)" & set/A "T=!T:-=8640000-!"
set/A "cc=T%%100+100,T/=100,ss=T%%60+100,T/=60,mm=T%%60+100,hh=T/60+100"
set "value=!hh:~1!%CE%!mm:~1!%CE%!ss:~1!%DE%!cc:~1!" & if "%~2"=="" echo/!value!
endlocal & set "timer_end=%value%" & set "timer_set=" & goto :eof
用法:
計時器& echo start_cmds & timeout /t 3 & echo end_cmds &計時器
計時器和計時器“23:23:23,00”
計時器“23:23:23,00”和計時器
計時器“13.23.23,00”和計時器“03:03:03.00”
timer & timer "0:00:00.00" no & cmd /v:on /c echo until深夜=!timer_end!
現在可以混合輸入,因為在執行期間那些不太可能但可能的時間格式更改
2:Function :定時器與批處理腳本捆綁在一起(示例用法如下):
@echo off
set "TIMER=call :timer" & rem short macro
echo.
echo EXAMPLE:
call :timer
timeout /t 3 >nul & rem Any process here..
call :timer
echo.
echo SHORT MACRO:
%TIMER% & timeout /t 1 & %TIMER%
echo.
echo TEST INPUT:
set "start=22:04:04.58"
set "end=04.22.44,22"
echo %start% ~ start & echo %end% ~ end
call :timer "%start%"
call :timer "%end%"
echo.
%TIMER% & %TIMER% "00:00:00.00" no
echo UNTIL MIDNIGHT: %timer_end%
echo.
pause
exit /b
::測試它,復制粘貼上面和下面的代碼部分
rem :AveYo: compact timer function with Regional format, 24-hours and mixed input support
:timer Usage " call :timer [input - optional] [no - optional]" :i Result printed on second call, saved to timer_end
if not defined timer_set (if not "%~1"=="" (call set "timer_set=%~1") else set "timer_set=%TIME: =0%") & goto :eof
(if not "%~1"=="" (call set "timer_end=%~1") else set "timer_end=%TIME: =0%") & setlocal EnableDelayedExpansion
for /f "tokens=1-6 delims=0123456789" %%i in ("%timer_end%%timer_set%") do (set CE=%%i&set DE=%%k&set CS=%%l&set DS=%%n)
set "TE=!timer_end:%DE%=%%100)*100+1!" & set "TS=!timer_set:%DS%=%%100)*100+1!"
set/A "T=((((10!TE:%CE%=%%100)*60+1!%%100)-((((10!TS:%CS%=%%100)*60+1!%%100)" & set/A "T=!T:-=8640000-!"
set/A "cc=T%%100+100,T/=100,ss=T%%60+100,T/=60,mm=T%%60+100,hh=T/60+100"
set "value=!hh:~1!%CE%!mm:~1!%CE%!ss:~1!%DE%!cc:~1!" & if "%~2"=="" echo/!value!
endlocal & set "timer_end=%value%" & set "timer_set=" & goto :eof
CE,DE 和 CS,DS 代表冒號結尾,點結尾和冒號集,點集 - 用於混合格式支持
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.