批处理延时方法小结:
1.ping
@echo off
:loop
echo %time%
ping 127.1 -n 2 1>nul
echo %time%
goto loop
内存使用: cmd.exe 1704k
ping.exe 2920k
误差评定: 较高
优点:
代码构造简单
缺点:
内存占用高,延迟时间长的话误差相对较大。
2.还是ping
@echo off
:loop
echo %time%
ping 1 -n 1 -w 1000 2>nul 1>nul
echo %time%
goto loop
内存使用: cmd.exe 1700k
ping.exe 2912k
误差评定: 一般
优点:
代码构造简单,时间越长误差越小,精确度较高(50ms)
缺点:
内存占用高
3.call
@echo off
:loop
echo %time%
call :delay 1000
echo %time%
goto loop
:delay
set /a num=num + 1
if %num% geq %1 (set num=) && goto :eof
goto :eof
内存使用: cmd.exe 1744k
cmd.exe 1740k
误差评定: 很高 (受CPU频率影响非常大,几乎无法准确把握全局延迟时间)
优点:
精确度较高
缺点:
不适合需精确把握时间的场合
4.msg
@echo off
:loop
echo %time%
msg %username% /time:20 /w "正在延时,点确定可以取消延时!"
echo %time%
goto loop
内存使用: cmd.exe 1752k
msg.exe 2620k
误差评定: 低
优点:
比较稳定,可中途取消延时,代码构造简单
缺点:
内存占用非常大,有窗口弹出(优点?缺点?)
5.vbs
@echo off
echo Wscript.Sleep WScript.Arguments(0) >%tmp%\delay.vbs
:loop
echo %time%
cscript //b //nologo %tmp%\delay.vbs 2000
echo %time%
goto loop
内存使用: cscript.exe 4812k
cmd.exe 1708k
误差评定: 很低
优点:
精确度最高,使用更灵活,方便
缺点:
产生临时文件,内存占用多
End:
1.测试条件有限,以上数据并不具备权威性。
2.关于精确度。
其实只要是参数可以指定时间的命令精确度都比较高,之所以受影响是取决与命令执行的次数,次数越多,精确度越低。而方案5中之所以说它的精确度最高,可以举个例子: cscript //b //nologo delay.vbs 2036,而方案二由于最小精确度的缘故,还是比方案5要差一点。
3.欢迎大家补充。
参考链接:
1.
从一个GIF文件引起的麻烦
2.
批处理编程的异类
Last edited by 3742668 on 2006-9-29 at 08:49 ]
Summary of Batch Processing Delay Methods:
1. ping
@echo off
:loop
echo %time%
ping 127.1 -n 2 1>nul
echo %time%
goto loop
Memory Usage: cmd.exe 1704k
ping.exe 2920k
Error Evaluation: High
Advantages:
Simple code structure
Disadvantages:
High memory usage, relatively large error when delay time is long
2. Still ping
@echo off
:loop
echo %time%
ping 1 -n 1 -w 1000 2>nul 1>nul
echo %time%
goto loop
Memory Usage: cmd.exe 1700k
ping.exe 2912k
Error Evaluation: General
Advantages:
Simple code structure, smaller error with longer time, higher accuracy (50ms)
Disadvantages:
High memory usage
3. call
@echo off
:loop
echo %time%
call :delay 1000
echo %time%
goto loop
:delay
set /a num=num + 1
if %num% geq %1 (set num=) && goto :eof
goto :eof
Memory Usage: cmd.exe 1744k
cmd.exe 1740k
Error Evaluation: Very high (greatly affected by CPU frequency, almost impossible to accurately grasp overall delay time)
Advantages:
Relatively high accuracy
Disadvantages:
Not suitable for occasions requiring precise time grasping
4. msg
@echo off
:loop
echo %time%
msg %username% /time:20 /w "Delaying, click OK to cancel delay!"
echo %time%
goto loop
Memory Usage: cmd.exe 1752k
msg.exe 2620k
Error Evaluation: Low
Advantages:
Relatively stable, can cancel delay midway, simple code structure
Disadvantages:
Very high memory usage, window pops up (advantage? Disadvantage?)
5. vbs
@echo off
echo Wscript.Sleep WScript.Arguments(0) >%tmp%\delay.vbs
:loop
echo %time%
cscript //b //nologo %tmp%\delay.vbs 2000
echo %time%
goto loop
Memory Usage: cscript.exe 4812k
cmd.exe 1708k
Error Evaluation: Very low
Advantages:
Highest accuracy, more flexible and convenient to use
Disadvantages:
Temporary file generated, high memory usage
End:
1. Limited test conditions, the above data is not authoritative.
2. About accuracy.
Actually, any command with adjustable time has relatively high accuracy, the reason for being affected depends on the number of command executions, the more times, the lower the accuracy. And the reason why the fifth scheme is said to have the highest accuracy can be exemplified: cscript //b //nologo delay.vbs 2036, while the second scheme is still worse than the fifth scheme due to the minimum accuracy.
3. Everyone is welcome to supplement.
Reference Links:
1.
Trouble Caused by a GIF File
2.
Heterodoxy in Batch Processing Programming
Last edited by 3742668 on 2006-9-29 at 08:49 ]