计算Windows批处理文件中的时差

Calculate time difference in Windows batch file

如何在批处理文件中获得两次差异? 因为我想在HTML文件中打印它。

我认为这是可能的,但事实并非如此。

1
2
3
Set"tijd=%time%"
echo %tijd%
echo %time%-%tijd%

结果:

1
2
11:07:48,85
11:16:58,99-11:07:48,85

但我想要的是:

1
00:09:10,14

或9分10秒或550秒


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@echo off

rem Get start time:
for /F"tokens=1-4 delims=:.," %%a in ("%time%") do (
   set /A"start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Any process here...

rem Get end time:
for /F"tokens=1-4 delims=:.," %%a in ("%time%") do (
   set /A"end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Get elapsed time:
set /A elapsed=end-start

rem Show elapsed time:
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
echo %hh%:%mm%:%ss%,%cc%

编辑2017-05-09:添加了更短的方法

我开发了一种更短的方法来获得相同的结果,所以我忍不住在这里发布它。用于分隔时间部分的两个for命令和用于在结果中插入前导零的三个if命令被两个长的算术表达式替换,甚至可以组合成一个更长的行。

该方法包括将具有"HH:MM:SS.CC"格式的时间的变量直接转换为将时间转换为厘秒所需的公式,相应地给出下面给出的映射方案:

1
2
3
       HH        :      MM        :      SS        .       CC

(((10  HH  %%100)*60+1  MM  %%100)*60+1  SS  %%100)*100+1  CC  %%100

也就是说,在开头插入(((10,用%%100)*60+1替换冒号,用%%100)*100+1替换点并在末尾插入%%100;最后,将结果字符串计算为算术表达式。在时间变量中,有两个不同的子串需要替换,因此转换必须以两行完成。要获得经过的时间,请使用(endTime)-(startTime)表达式并替换同一行中的两个时间字符串。

编辑2017/06/14:增加了区域独立调整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@echo off
setlocal EnableDelayedExpansion

set"startTime=%time: =0%"

set /P"=Any process here..."

set"endTime=%time: =0%"



rem Get elapsed time:
set"end=!endTime:%time:~8,1%=%%100)*100+1!"  &  set"start=!startTime:%time:~8,1%=%%100)*100+1!"
set /A"elap=((((10!end:%time:~2,1%=%%100)*60+1!%%100)-((((10!start:%time:~2,1%=%%100)*60+1!%%100)"

rem Convert elapsed time to HH:MM:SS:CC format:
set /A"cc=elap%%100+100,elap/=100,ss=elap%%60+100,elap/=60,mm=elap%%60+100,hh=elap/60+100"

echo Start:    %startTime%
echo End:      %endTime%
echo Elapsed:  %hh:~1%%time:~2,1%%mm:~1%%time:~2,1%%ss:~1%%time:~8,1%%cc:~1%

您可以在此答案中查看此方法的详细说明。


在这里回答:
如何使用Windows批处理文件来衡量控制台应用程序的性能?

批量"程序"下面应该做你想要的。请注意,它以厘秒而不是毫秒输出数据。使用命令的精度只有几厘秒。

这是一个示例输出:

1
2
3
4
5
6
STARTTIME: 13:42:52,25
ENDTIME: 13:42:56,51
STARTTIME: 4937225 centiseconds
ENDTIME: 4937651 centiseconds
DURATION: 426 in centiseconds
00:00:04,26

这是批处理脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@echo off
setlocal

rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
set STARTTIME=%TIME%

rem here begins the command you want to measure
dir /s > nul
rem here ends the command you want to measure

set ENDTIME=%TIME%

rem output as time
echo STARTTIME: %STARTTIME%
echo ENDTIME: %ENDTIME%

rem convert STARTTIME and ENDTIME to centiseconds
set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)

rem calculating the duratyion is easy
set /A DURATION=%ENDTIME%-%STARTTIME%

rem we might have measured the time inbetween days
if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%

rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds
set /A DURATIONH=%DURATION% / 360000
set /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000
set /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100
set /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100)

rem some formatting
if %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH%
if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM%
if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS%
if %DURATIONHS% LSS 10 set DURATIONHS=0%DURATIONHS%

rem outputing
echo STARTTIME: %STARTTIME% centiseconds
echo ENDTIME: %ENDTIME% centiseconds
echo DURATION: %DURATION% in centiseconds
echo %DURATIONH%:%DURATIONM%:%DURATIONS%,%DURATIONHS%

endlocal
goto :EOF


Aacini代码的重新哈希,因为很可能您将开始时间设置为变量并希望将该数据保存为输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    @echo off

    rem ******************  MAIN CODE SECTION
    set STARTTIME=%TIME%

    rem Your code goes here (remove the ping line)
    ping -n 4 -w 1 127.0.0.1 >NUL

    set ENDTIME=%TIME%

    rem ******************  END MAIN CODE SECTION


    rem Change formatting for the start and end times
    for /F"tokens=1-4 delims=:.," %%a in ("%STARTTIME%") do (
       set /A"start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
    )

    for /F"tokens=1-4 delims=:.," %%a in ("%ENDTIME%") do (
       set /A"end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
    )

    rem Calculate the elapsed time by subtracting values
    set /A elapsed=end-start

    rem Format the results for output
    set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
    if %hh% lss 10 set hh=0%hh%
    if %mm% lss 10 set mm=0%mm%
    if %ss% lss 10 set ss=0%ss%
    if %cc% lss 10 set cc=0%cc%

    set DURATION=%hh%:%mm%:%ss%,%cc%

    echo Start    : %STARTTIME%
    echo Finish   : %ENDTIME%
    echo          ---------------
    echo Duration : %DURATION%

输出:

1
2
3
4
    Start    : 11:02:45.92
    Finish   : 11:02:48.98
             ---------------
    Duration : 00:00:03,06


这是我尝试批量测量时差。

它遵循%TIME%的区域格式,而不对时间和小数分隔符的字符类型进行任何假设。

代码已注释,但我也将在此处进行描述。

它非常灵活,因此也可用于标准化非标准时间值

主要功能:timediff

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
:: timediff
:: Input and output format is the same format as %TIME%
:: If EndTime is less than StartTime then:
::   EndTime will be treated as a time in the next day
::   in that case, function measures time difference between a maximum distance of 24 hours minus 1 centisecond
::   time elements can have values greater than their standard maximum value ex: 12:247:853.5214
::   provided than the total represented time does not exceed 24*360000 centiseconds
::   otherwise the result will not be meaningful.
:: If EndTime is greater than or equals to StartTime then:
::   No formal limitation applies to the value of elements,
::   except that total represented time can not exceed 2147483647 centiseconds.

:timediff <outDiff> <inStartTime> <inEndTime>
(
    setlocal EnableDelayedExpansion
    set"Input=!%~2! !%~3!"
    for /F"tokens=1,3 delims=0123456789" %%A in ("!Input!") do set"time.delims=%%A%%B"
)
for /F"tokens=1-8 delims=%time.delims%" %%a in ("%Input%") do (
    for %%A in ("@h1=%%a""@m1=%%b""@s1=%%c""@c1=%%d""@h2=%%e""@m2=%%f""@s2=%%g""@c2=%%h") do (
        for /F"tokens=1,2 delims==" %%A in ("%%~A") do (
            for /F"tokens=* delims=0" %%B in ("%%B") do set"%%A=%%B"
        )
    )
    set /a"@d=(@h2-@h1)*360000+(@m2-@m1)*6000+(@s2-@s1)*100+(@c2-@c1), @sign=(@d>>31)&1, @d+=(@sign*24*360000), @h=(@d/360000), @d%%=360000, @m=@d/6000, @d%%=6000, @s=@d/100, @c=@d%%100"
)
(
    if %@h% LEQ 9 set"@h=0%@h%"
    if %@m% LEQ 9 set"@m=0%@m%"
    if %@s% LEQ 9 set"@s=0%@s%"
    if %@c% LEQ 9 set"@c=0%@c%"
)
(
    endlocal
    set"%~1=%@h%%time.delims:~0,1%%@m%%time.delims:~0,1%%@s%%time.delims:~1,1%%@c%"
    exit /b
)

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@echo off
setlocal EnableExtensions
set"TIME="

set"Start=%TIME%"
REM Do some stuff here...
set"End=%TIME%"

call :timediff Elapsed Start End
echo Elapsed Time: %Elapsed%

pause
exit /b

:: put the :timediff function here

解释:timediff函数:

1
function prototype  :timediff <outDiff> <inStartTime> <inEndTime>

输入和输出格式与%TIME%的格式相同

它从左到右需要3个参数:

Param1:要将结果保存到的环境变量的名称。
Param2:要传递给包含StartTime字符串的函数的环境变量的名称
Param3:要传递给包含EndTime字符串的函数的环境变量的名称

如果EndTime小于StartTime,则:

    EndTime将被视为第二天的时间
    在这种情况下,该功能测量最大距离为24小时减去1厘秒之间的时间差
    时间元素的值可以大于其标准最大值ex:12:247:853.5214
    提供的总时间不超过24 * 360000厘秒或(24:00 00.00),否则结果将没有意义。

如果EndTime大于或等于StartTime,则:

    没有正式的限制适用于元素的价值,
    除了总代表时间不能超过2147483647厘秒。

更多具有文字和非标准时间值的示例

    EndTime小于StartTime的文字示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@echo off
setlocal EnableExtensions

set"start=23:57:33,12"
set"end=00:02:19,41"

call :timediff dif start end

echo Start Time: %start%
echo End Time:   %end%
echo,
echo Difference: %dif%
echo,

pause
exit /b

:: put the :timediff function here
    输出:
1
2
3
4
Start Time: 23:57:33,12
End Time:   00:02:19,41

Difference: 00:04:46,29
    规范化非标准时间:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@echo off
setlocal EnableExtensions

set"start=00:00:00.00"
set"end=27:2457:433.85935"

call :timediff normalized start end

echo,
echo %end% is equivalent to %normalized%
echo,

pause
exit /b

:: put the :timediff function here
    输出:
1
27:2457:433.85935 is equivalent to 68:18:32.35
    最后的奖金示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@echo off
setlocal EnableExtensions

set"start=00:00:00.00"
set"end=00:00:00.2147483647"

call :timediff normalized start end

echo,
echo 2147483647 centiseconds equals to %normalized%
echo,

pause
exit /b

:: put the :timediff function here
    输出:
1
2147483647 centiseconds equals to 5965:13:56.47

Aacini的最新代码展示了一种非常棒的变量替换方法。
遗憾的是,这不是区域格式证明 - 它在很多层面都失败了。
这是一个简短的修复,可以保持替换+数学方法的完整性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@echo off
setlocal EnableDelayedExpansion

set"startTime=%time: =0%" & rem AveYo: fix single digit hour

set /P"=Any process here..."

set"endTime=%time: =0%" & rem AveYo: fix single digit hour

rem Aveyo: Regional format fix with just one aditional line
for /f"tokens=1-3 delims=0123456789" %%i in ("%endTime%") do set"COLON=%%i" & set"DOT=%%k"

rem Get elapsed time:
set"end=!endTime:%DOT%=%%100)*100+1!" & set"start=!startTime:%DOT%=%%100)*100+1!"
set /A"elap=((((10!end:%COLON%=%%100)*60+1!%%100)-((((10!start:%COLON%=%%100)*60+1!%%100)"

rem Aveyo: Fix 24 hours
set /A"elap=!elap:-=8640000-!"

rem Convert elapsed time to HH:MM:SS:CC format:
set /A"cc=elap%%100+100,elap/=100,ss=elap%%60+100,elap/=60,mm=elap%%60+100,hh=elap/60+100"

echo Start:    %startTime%
echo End:      %endTime%
echo Elapsed:  %hh:~1%%COLON%%mm:~1%%COLON%%ss:~1%%DOT%%cc:~1% & rem AveYo: display as regional
pause

*

具有区域格式,24小时和混合输入支持的"精益和平均"TIMER
适应Aacini的替换方法体,没有IF,只有一个FOR(我的区域修复)

1:文件timer.bat放在%PATH%或当前目录的某处

1
2
3
4
5
6
7
8
9
@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

用法:
计时器&amp; echo start_cmds&amp;超时/ t 3&amp; echo end_cmds&amp;计时器
计时器&amp;计时器"23:23:23,00"
计时器"23:23:23,00"&amp;计时器
计时器"13.23.23,00"&amp;计时器"03:03:03.00"
计时器&amp;计时器"0:00:00.00"no&amp; cmd / v:on / c echo直到午夜=!timer_end!
输入现在可以混合,对于那些不太可能的,但在执行期间可能的时间格式更改

2:功能:与批处理脚本捆绑在一起的计时器(下面的示例用法):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@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

::测试它,复制粘贴代码部分的上方和下方

1
2
3
4
5
6
7
8
9
10
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

修复了Gynnad领先的0问题。我用两条线修好了

1
2
SET STARTTIME=%STARTTIME: =0%
SET ENDTIME=%ENDTIME: =0%

完整脚本(CalculateTime.cmd):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@ECHO OFF

:: F U N C T I O N S

:__START_TIME_MEASURE
SET STARTTIME=%TIME%
SET STARTTIME=%STARTTIME: =0%
EXIT /B 0

:__STOP_TIME_MEASURE
SET ENDTIME=%TIME%
SET ENDTIME=%ENDTIME: =0%
SET /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
SET /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)
SET /A DURATION=%ENDTIME%-%STARTTIME%
IF %DURATION% == 0 SET TIMEDIFF=00:00:00,00 && EXIT /B 0
IF %ENDTIME% LSS %STARTTIME% SET /A DURATION=%STARTTIME%-%ENDTIME%
SET /A DURATIONH=%DURATION% / 360000
SET /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000
SET /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100
SET /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100)
IF %DURATIONH% LSS 10 SET DURATIONH=0%DURATIONH%
IF %DURATIONM% LSS 10 SET DURATIONM=0%DURATIONM%
IF %DURATIONS% LSS 10 SET DURATIONS=0%DURATIONS%
IF %DURATIONHS% LSS 10 SET DURATIONHS=0%DURATIONHS%
SET TIMEDIFF=%DURATIONH%:%DURATIONM%:%DURATIONS%,%DURATIONHS%
EXIT /B 0

:: U S A G E

:: Start Measuring
CALL :__START_TIME_MEASURE

:: Print Message on Screen without Linefeed
ECHO|SET /P=Execute Job...

:: Some Time pending Jobs here
:: '> NUL 2>&1' Dont show any Messages or Errors on Screen
MyJob.exe > NUL 2>&1

:: Stop Measuring
CALL :__STOP_TIME_MEASURE

:: Finish the Message 'Execute Job...' and print measured Time
ECHO [Done] (%TIMEDIFF%)

:: Possible Result
:: Execute Job... [Done] (00:02:12,31)
:: Between 'Execute Job... ' and '[Done] (00:02:12,31)' the Job will be executed


根据以前的答案,这里有可重复使用的"程序"和计算经过时间的用法示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@echo off
setlocal

set starttime=%TIME%
echo Start Time: %starttime%

REM ---------------------------------------------
REM --- PUT THE CODE YOU WANT TO MEASURE HERE ---
REM ---------------------------------------------

set endtime=%TIME%
echo End Time: %endtime%
call :elapsed_time %starttime% %endtime% duration
echo Duration: %duration%

endlocal
echo on & goto :eof

REM --- HELPER PROCEDURES ---

:time_to_centiseconds
:: %~1 - time
:: %~2 - centiseconds output variable
setlocal
set _time=%~1
for /F"tokens=1-4 delims=:.," %%a in ("%_time%") do (
   set /A"_result=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
endlocal & set %~2=%_result%
goto :eof

:centiseconds_to_time
:: %~1 - centiseconds
:: %~2 - time output variable
setlocal
set _centiseconds=%~1
rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds
set /A _h=%_centiseconds% / 360000
set /A _m=(%_centiseconds% - %_h%*360000) / 6000
set /A _s=(%_centiseconds% - %_h%*360000 - %_m%*6000) / 100
set /A _hs=(%_centiseconds% - %_h%*360000 - %_m%*6000 - %_s%*100)
rem some formatting
if %_h% LSS 10 set _h=0%_h%
if %_m% LSS 10 set _m=0%_m%
if %_s% LSS 10 set _s=0%_s%
if %_hs% LSS 10 set _hs=0%_hs%
set _result=%_h%:%_m%:%_s%.%_hs%
endlocal & set %~2=%_result%
goto :eof

:elapsed_time
:: %~1 - time1 - start time
:: %~2 - time2 - end time
:: %~3 - elapsed time output
setlocal
set _time1=%~1
set _time2=%~2
call :time_to_centiseconds %_time1% _centi1
call :time_to_centiseconds %_time2% _centi2
set /A _duration=%_centi2%-%_centi1%
call :centiseconds_to_time %_duration% _result
endlocal & set %~3=%_result%
goto :eof


使用单一功能,可以使用自定义测量单位或格式化。
每次调用函数时都没有参数,我们重新启动了初始时间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
@ECHO OFF

ECHO.
ECHO DEMO timer function
ECHO --------------------

SET DELAY=4

:: First we call the function without any parameters to set the starting time
CALL:timer

:: We put some code we want to measure
ECHO.
ECHO Making some delay, please wait...
ECHO.

ping -n %DELAY% -w 1 127.0.0.1 >NUL

:: Now we call the function again with the desired parameters

CALL:timer elapsed_time

ECHO by Default : %elapsed_time%

CALL:timer elapsed_time"s"

ECHO in Seconds : %elapsed_time%

CALL:timer elapsed_time"anything"

ECHO Formatted  : %elapsed_time%  (HH:MM:SS.CS)

ECHO.
PAUSE


:: Elapsed Time Function
:: -----------------------------------------------------------------------
:: The returned value is in centiseconds, unless you enter the parameters
:: to be in another unit of measure or with formatted
::
::  Parameters:
::             <return>     the returned value
::             [formatted]  s (for seconds), m (for minutes), h (for hours)
::                          anything else for formatted output
:: -----------------------------------------------------------------------
:timer <return> [formatted]
    SetLocal EnableExtensions EnableDelayedExpansion

    SET _t=%time%
    SET _t=%_t::0=: %
    SET _t=%_t:,0=, %
    SET _t=%_t:.0=. %
    SET _t=%_t:~0,2% * 360000 + %_t:~3,2% * 6000 + %_t:~6,2% * 100 + %_t:~9,2%
    SET /A _t=%_t%

    :: If we call the function without parameters is defined initial time
    SET _r=%~1
    IF NOT DEFINED _r (
        EndLocal & SET TIMER_START_TIME=%_t% & GOTO :EOF
    )

    SET /A _t=%_t% - %TIMER_START_TIME%

    :: In the case of wanting a formatted output
    SET _f=%~2
    IF DEFINED _f (

        IF"%_f%" =="s" (

            SET /A"_t=%_t% / 100"

        ) ELSE (
            IF"%_f%" =="m" (

                SET /A"_t=%_t% / 6000"

            ) ELSE (

                IF"%_f%" =="h" (

                    SET /A"_t=%_t% / 360000"

                ) ELSE (

                    SET /A"_h=%_t% / 360000"
                    SET /A"_m=(%_t% - !_h! * 360000) / 6000"
                    SET /A"_s=(%_t% - !_h! * 360000 - !_m! * 6000) / 100"
                    SET /A"_cs=(%_t% - !_h! * 360000 - !_m! * 6000 - !_s! * 100)"

                    IF !_h! LSS 10 SET"_h=0!_h!"
                    IF !_m! LSS 10 SET"_m=0!_m!"
                    IF !_s! LSS 10 SET"_s=0!_s!"
                    IF !_cs! LSS 10 SET"_cs=0!_cs!"
                    SET"_t=!_h!:!_m!:!_s!.!_cs!"
                    SET"_t=!_t:00:=!"

                )
            )
        )
    )

    EndLocal & SET %~1=%_t%
goto :EOF

延迟94秒的测试

1
2
3
4
5
6
7
8
9
10
DEMO timer function
--------------------

Making some delay, please wait...

by Default : 9404
in Seconds : 94
Formatted  : 01:34.05  (HH:MM:SS.CS)

Presione una tecla para continuar . . .