How long does a batch file take to execute?
如何编写脚本来计算脚本完成的时间?
我以为情况确实如此,但显然不是......
1 2 3 4 5 6 7 8 | @echo off set starttime=%time% set endtime=%time% REM do stuff here set /a runtime=%endtime%-%starttime% echo Script took %runtime% to complete |
有关原始批处理文件的两件事情有所突破。 但从长远来看都不会有帮助。 strike>
无论您的基准测试方法是什么,请务必捕获操作前的开始时间和操作后的结束时间。你的样本没有这样做。
我打算说它无法完成,但是批处理语言比它的功能强大得多,所以这里有一个简单的TIMER.BAT实现。为了记录,Pax打败了我的答案,显示我在摆弄时字符串分裂,Johannes R?ssel建议将算术移到测量区域之外:
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 | @echo off setlocal rem Remeber start time. Note that we don't look at the date, so this rem calculation won't work right if the program run spans local midnight. set t0=%time: =0% rem do something here.... but probably with more care about quoting. rem specifically, odd things will happen if any arguments contain rem precent signs or carets and there may be no way to prevent it. %* rem Capture the end time before doing anything else set t=%time: =0% rem make t0 into a scaler in 100ths of a second, being careful not rem to let SET/A misinterpret 08 and 09 as octal set /a h=1%t0:~0,2%-100 set /a m=1%t0:~3,2%-100 set /a s=1%t0:~6,2%-100 set /a c=1%t0:~9,2%-100 set /a starttime = %h% * 360000 + %m% * 6000 + 100 * %s% + %c% rem make t into a scaler in 100ths of a second set /a h=1%t:~0,2%-100 set /a m=1%t:~3,2%-100 set /a s=1%t:~6,2%-100 set /a c=1%t:~9,2%-100 set /a endtime = %h% * 360000 + %m% * 6000 + 100 * %s% + %c% rem runtime in 100ths is now just end - start set /a runtime = %endtime% - %starttime% set runtime = %s%.%c% echo Started at %t0% echo Ran for %runtime%0 ms |
您可以通过不打扰第二部分的第100个部分来简化算术并对其整体准确性更加诚实。假设您有一个睡眠命令或其他时间浪费,它在行动中:
1 2 3 | C:> TIMER SLEEP 3 Script took 3000 ms to complete C:> |
编辑:我修改了评论中建议的代码及其描述。
我认为当NT团队用CMD.EXE取代COMMAND.COM时,他们认为他们不会让它变得非常不同。但实际上,它几乎是一种全新的语言。如果启用了扩展,许多旧的收藏命令都具有新功能。
其中一个是
对于一个真正令人震惊的,请看一下set的完整描述(在提示符下尝试
编辑2:修复了Frankie在评论中报告的包含
请注意,这里有一个明显的疏忽,我可能不会解决。如果命令在不同的日期开始而不是结束,它将无法工作。也就是说,它会做一些与时间相关的数学并报告差异,但差异并不大。
修复它至少可以警告这种情况很容易。修复它以做正确的事情更难。
编辑3:修复了错误,其中%h%未正确设置为单位数小时。这是由于%time%返回"9:01:23.45"。注意空间。使用%time:= 0%将空格替换为前导零,并且%h%将被正确设置。仅当脚本从一个数字小时运行到下一个小时时才会发生此错误。
这有点切,但它可能会帮助你。
Microsoft有一个timeit.exe程序,它或多或少像unix'time'命令的增强版本。它出现在Windows Server 2003资源工具包中,它几乎已经取代了我想要做的事情,就像你建议的那样。
这可能值得一看。
优秀的小常规。 但是,如果计算跨越两天,则计算不正确。
结果需要从24小时减去(8640000厘秒)。
同时删除相关行中的一个重复的"set"命令。
请注意,区域设置会影响TIME函数的格式,小数是英国的完整停止而不是逗号。
线条
我们可能已经测量了几天之间的时间
1 | if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME% |
需要改变
我们可能已经测量了几天的时间
1 | if %ENDTIME% LSS %STARTTIME% set /A DURATION=8640000 - (%STARTTIME% - %ENDTIME%) |
查看此脚本将通过WMI检索时间,以便区域设置独立。
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 | @echo off :::::::::::::::::::::::::::::::::::::::::: :: TimeDiff v1.00 by LEVENTE ROG :: :: www.thesysadminhimself.com :: :::::::::::::::::::::::::::::::::::::::::: ::[ EULA ]::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Feel free to use this script. The code can be redistributed :: :: and edited, but please keep the credits. :: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::[ CHANGELOG ]:::::::::::::: :: v1.00 - First Version :: ::::::::::::::::::::::::::::: FOR /F"skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r"."') DO ( set Milisecond=%time:~9,2% set Day=%%A set Hour=%%B set Minute=%%C set Second=%%D ) set /a Start=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond% :: :: :: PUT COMMANDS HERE ping www.thesysadminhimself.com :: :: FOR /F"skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r"."') DO ( set Day=%%A set Hour=%%B set Minute=%%C set Second=%%D ) set Milisecond=%time:~9,2% set /a End=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond% set /a Diff=%End%-%Start% set /a DiffMS=%Diff%%%100 set /a Diff=(%Diff%-%DiffMS%)/100 set /a DiffSec=%Diff%%%60 set /a Diff=(%Diff%-%Diff%%%60)/60 set /a DiffMin=%Diff%%%60 set /a Diff=(%Diff%-%Diff%%%60)/60 set /a DiffHrs=%Diff% :: format with leading zeroes if %DiffMS% LSS 10 set DiffMS=0%DiffMS!% if %DiffSec% LSS 10 set DiffMS=0%DiffSec% if %DiffMin% LSS 10 set DiffMS=0%DiffMin% if %DiffHrs% LSS 10 set DiffMS=0%DiffHrs% echo %DiffHrs%:%DiffMin%:%DiffSec%.%DiffMS% |
我个人的偏好是安装Cygwin并使用
以下脚本将为10秒的ping命令计时,该命令能够越过单日边界而不会被负数绑定。稍微增强将允许它跨越许多天的边界。
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 | @echo off setlocal enableextensions enabledelayedexpansion set starttime=%time% ping -n 11 127.0.0.1 >nul: 2>nul: set endtime=%time% set /a hrs=%endtime:~0,2% set /a hrs=%hrs%-%starttime:~0,2% set /a mins=%endtime:~3,2% set /a mins=%mins%-%starttime:~3,2% set /a secs=%endtime:~6,2% set /a secs=%secs%-%starttime:~6,2% if %secs% lss 0 ( set /a secs=!secs!+60 set /a mins=!mins!-1 ) if %mins% lss 0 ( set /a mins=!mins!+60 set /a hrs=!hrs!-1 ) if %hrs% lss 0 ( set /a hrs=!hrs!+24 ) set /a tot=%secs%+%mins%*60+%hrs%*3600 echo End = %endtime% echo Start = %starttime% echo Hours = %hrs% echo Minutes = %mins% echo Seconds = %secs% echo Total = %tot% endlocal |
你不能直接在批处理脚本中进行时间算术,所以你需要一个外部程序来计算时差,或者提取每个部分的时间并以这种方式算术。
请查看此论坛主题的底部,了解如何执行后者的示例。摘录于此:
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 | @echo off cls REM ================================================== ====== REM = Setting Date Time Format = REM ================================================== ====== set DT=%DATE% %TIME% set year=%DT:~10,4% set mth=%DT:~4,2% set date=%DT:~7,2% set hour=%DT:~15,2% set min=%DT:~18,2% set sec=%DT:~21,2% set newDT=%year%_%mth%_%date% %hour%%min%%sec% set hour=14 set min=10 REM =============================== REM = Getting End Time = REM =============================== set EndTime=%TIME% set EndHour=%EndTime:~0,2% set EndMin=%EndTime:~3,2% REM =============================== REM = Finding Difference = REM =============================== set /a Hour_Diff=EndHour - hour >nul set /a Min_Diff=EndMin - min >nul REM =============================== REM = Hour Hand Change? = REM =============================== IF [%Hour_Diff]==[0] ( Set Duration=%Min_Diff% ) ELSE ( Set /a Duration=60-%Min_Diff% >nul ) echo Start Time = %hour% : %min% echo End Time = %EndHour% : %EndMin% echo. echo Min Diff = %Min_Diff% echo. echo time difference (mins) = %Duration% |
我也认为这是一个错字,但你确实希望在处理后设置