Windows 如何度量for循环每次迭代所需的时间?

Windows 如何度量for循环每次迭代所需的时间?,windows,batch-file,for-loop,time,batch-processing,Windows,Batch File,For Loop,Time,Batch Processing,如何使用这样的脚本记录批处理脚本中for循环每次迭代所需的时间?我实现它是为了记录整个脚本的执行时间,但我希望获得for循环每次迭代的持续时间 @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

如何使用这样的脚本记录批处理脚本中for循环每次迭代所需的时间?我实现它是为了记录整个脚本的执行时间,但我希望获得for循环每次迭代的持续时间

    @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% 
输出:

    Start    : 11:02:45.92
    Finish   : 11:02:48.98
             ---------------
    Duration : 00:00:03,06
编辑:这就是我的实现的外观

@ECHO on
setlocal enabledelayedexpansion
cls

RMDIR c:\path1 /s /q
mkdir c:\path1
xcopy "\\path2\*" c:\path1 /s /i
cd c:\path3

for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
cd c:\path1

sqlcmd -S server -i "\\path2\DropDatabases.sql"

for /D /r %%F IN ("*") DO ( 
    for %%G  IN ("%%F\*.extenstion1") DO xcopy "%%G" c:\path2\%newest% /y /i

    for /l %%A in (1,1,5) do (
        set STARTTIME=!TIME!

    for /f "delims=_" %%J IN ('forfiles /p "%%F" /m *.jmpt /c "cmd /c echo @path"')  DO start "program"  /D "c:\path3" /Wait program -r  %%J

    set ENDTIME=!TIME!
call :GetDuration !STARTTIME! !ENDTIME! 
    )
)

    exit /b
    :GetDuration
    set function_starttime=%1
    set function_endtime=%2

    rem Change formatting for the start and end times
    for /F "tokens=1-4 delims=:.," %%a in ("%function_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 ("%function_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    : %function_starttime%
    echo Finish   : %function_endtime%
    echo          ---------------
    echo Duration : %DURATION%
    echo.

sqlcmd -S server -i "\\path2\query.sql"

pause

在for循环代码块内部的任意一端设置starttime和endtime变量,并将所有转换和输出代码放入函数中。您还需要启用延迟扩展

@echo off
setlocal enabledelayedexpansion
cls

rem ******************  MAIN CODE SECTION
for /l %%A in (1,1,5) do (
    set STARTTIME=!TIME!

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

    set ENDTIME=!TIME!
    call :GetDuration !STARTTIME! !ENDTIME!
)

rem ******************  END MAIN CODE SECTION
exit /b

:GetDuration
set function_starttime=%1
set function_endtime=%2

rem Change formatting for the start and end times
for /F "tokens=1-4 delims=:.," %%a in ("%function_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 ("%function_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    : %function_starttime%
echo Finish   : %function_endtime%
echo          ---------------
echo Duration : %DURATION%
echo.

有一个非常好的包叫做“tictoc” 只需将tic(i)放在循环的开头,将toc()放在循环的结尾。在每次迭代之后,您都可以看到它所花费的精确时间(以秒为单位)

更多信息可以在这里找到


好吧,我有点困惑。我几乎没有使用批处理函数的经验,您是在该代码中创建函数还是它们只是占位符?从
:GetDuration
到脚本末尾的所有内容都是GetDuration函数。你用
call
调用它,就像它自己的脚本一样。参数也以同样的方式工作。函数通常以命令
goto:eof
结束,该命令到达文件的末尾,但在本例中,由于它是脚本中的最后一项内容,因此我可以将其禁用。那么
:GetDuration
是否应该跳出循环?是的!我提供的代码是一个功能完整的示例。为了使它符合您的需要,您所要做的就是用您的代码替换
ping
命令(并更改循环类型)。好的,还有一个小问题,如果我要测量的循环实际上在另一个for循环中,该怎么办?在链接中键入错误。它是
https://cran.r-project.org/web/packages/tictoc/tictoc.pdf
。它是关于
R
(与批处理文件
无关)