Board logo

标题: [特效代码] 如何优化批处理matrix效果效率? [打印本页]

作者: raymai97    时间: 2011-10-26 01:24     标题: 如何优化批处理matrix效果效率?

这几天做了matrix效果的批处理,代码是自己花很多时间想出来的~
无奈从上到下的matrix和从下到上的matrix,看似同样,需用的时间却相差很多
从下到上,无需cls,直接echo一条又一条,速度很快,不用for /l %%i in (1 1 200) do ver>nul来延时的话,基本秒刷

从上到下就不同了,根本不需延时,本身就很慢,而且得一直cls cls再cls。
为了增加效率,我还把call给废了,努力改成没有call的~
我的思路是,一直改变第一行的东西,改变前把第一行的东西搬来第二行,第二行的东西搬来第三行…… 以此类推~

我就想问,有没有更好的办法/思路呢?如果用那个神马tab退格符,是否就不用cls,直接echo,效率会比较高呢?

以下是我写的代码~

从下到上:
  1. @echo off
  2. color 2a
  3. title MATRIX CODE by Raymai97 [low cpu, normal, from down to up]
  4. mode con cols=80 lines=25
  5. setlocal enabledelayedexpansion
  6. :resetani
  7. for /l %%i in (1 1 25) do (set l%%i=)
  8. for /l %%i in (1 1 25) do (
  9. for /l %%j in (1 1 39) do (
  10. set /a ran=!random! %% 3
  11. if "!ran!"=="2" set "ran= "
  12. set l%%i=!ran! !l%%i!
  13. )
  14. )
  15. :b
  16. for /l %%i in (1 1 25) do (
  17. echo. !l%%i!
  18. if "!aniinit!"=="1" (for /l %%i in (1 1 220) do ver>nul)
  19. )
  20. set aniinit=1
  21. goto b
复制代码
从上到下:
  1. @echo off
  2. title MATRIX CODE by Raymai97 [high cpu, normal, from up to down]
  3. color 2a
  4. mode con cols=80 lines=25
  5. setlocal enabledelayedexpansion
  6. for /l %%i in (1 1 25) do (
  7. for /l %%j in (1 1 39) do (
  8. set /a ran=!random! %% 3
  9. if "!ran!"=="2" set "ran= "
  10. set l%%i=!ran! !l%%i!
  11. )
  12. )
  13. :disp
  14. cls
  15. set l1=
  16. for /l %%i in (1 1 39) do (
  17. set /a ran=!random! %% 3
  18. if "!ran!"=="2" set "ran= "
  19. set l1=!ran! !l1!
  20. )
  21. for /l %%i in (1 1 24) do (echo. !l%%i!)
  22. set num=25
  23. set num2=0
  24. :change
  25. if "!num2!"=="1" goto disp
  26. set /a num2=!num!-1
  27. set l!num!=!l%num2%!
  28. set /a num-=1
  29. goto change
复制代码

作者: defanive    时间: 2011-10-26 08:17

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "char= 01"
  4. for /l %%a in (1,1,160) do set "data=!data!            "
  5. :loop
  6. cls
  7. echo\%data:~0,-1%
  8. set "list="&set "data=%data:~,-80%"
  9. for /l %%a in (1,1,40) do set /a "i=!random!%%3"&set "list=!list! !i!"
  10. for %%a in (!list!) do set "data=!char:~%%a,1! !data!"
  11. goto :loop
复制代码
纯当练手,晚点再想一下弄快一点
作者: raymai97    时间: 2011-10-26 10:30

回复 2# defanive


高~ 直接先设置多多的空格,再用截取方法更改一部分的资料,然后一次echo完
只是更改窗口大小后就不好使了~

再继续研究看看~
作者: caruko    时间: 2011-10-26 10:50

刚想说,直接刷屏覆盖就可以,不用CLS。
不过2楼代码都已经有了
作者: defanive    时间: 2011-10-26 11:44

回复 4# caruko
还是得用cls,echo就算一次刷屏输出还是会有闪的问题,不知道为什么挺奇怪的,怀疑是输出的时候自动拖滚动条导致的闪
作者: Taurus    时间: 2012-11-26 17:50

效能測試?

1.bat是1樓的 , 2.bat是2樓的 , 3.bat是我的

1.bat
  1. @echo off
  2. color 2a
  3. mode con cols=80 lines=25
  4. setlocal enabledelayedexpansion
  5. for /l %%i in (1 1 25) do (
  6. for /l %%j in (1 1 39) do (
  7. set /a ran=!random! %% 3
  8. if "!ran!"=="2" set "ran= "
  9. set l%%i=!ran! !l%%i!
  10. )
  11. )
  12. set sec=%time:~6,2%
  13. :disp
  14. set /a looptime+=1
  15. if not "%time:~6,2%"=="%sec%" (
  16.     set sec=%time:~6,2%&title  !looptime! loop/1sec"
  17.     set/a looptime=0
  18. )
  19. cls
  20. set l1=
  21. for /l %%i in (1 1 39) do (
  22. set /a ran=!random! %% 3
  23. if "!ran!"=="2" set "ran= "
  24. set l1=!ran! !l1!
  25. )
  26. for /l %%i in (1 1 24) do (echo. !l%%i!)
  27. set num=25
  28. set num2=0
  29. :change
  30. if "!num2!"=="1" goto disp
  31. set /a num2=!num!-1
  32. set l!num!=!l%num2%!
  33. set /a num-=1
  34. goto change
复制代码
2.bat
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "char= 01"
  4. for /l %%a in (1,1,160) do set "data=!data!            "
  5. set sec=%time:~6,2%
  6. :loop
  7. set /a looptime+=1
  8. if not "%time:~6,2%"=="%sec%" (
  9.     set sec=%time:~6,2%&title  !looptime! loop/1sec"
  10.     set/a looptime=0
  11. )
  12. cls
  13. echo\%data:~0,-1%
  14. set "list="&set "data=%data:~,-80%"
  15. for /l %%a in (1,1,40) do set /a "i=!random!%%3"&set "list=!list! !i!"
  16. for %%a in (!list!) do set "data=!char:~%%a,1! !data!"
  17. goto :loop
复制代码
3.bat
  1. @echo off&setlocal EnableDelayedExpansion&color 0a&mode con: cols=80 lines=26
  2. set a=1234567890 abcd  e g hijk l nopqrst uvw x zAB C E F  IJ KL MNOP  RS T UV X YZ     
  3. set sec=%time:~6,2%
  4. :loop
  5. set /a num1=!random!%%80,num2=!random!%%80,num3=!random!%%80,num4=!random!%%80,num5=!random!%%80,num6=!random!%%80,num7=!random!%%80,num8=!random!%%80,num9=!random!%%80,num10=!random!%%80,num11=!random!%%80,num12=!random!%%80,num13=!random!%%80,num14=!random!%%80,num15=!random!%%80,num16=!random!%%80,num17=!random!%%80,num18=!random!%%80,num19=!random!%%80,num20=!random!%%80,num21=!random!%%80,num22=!random!%%80,num23=!random!%%80,num24=!random!%%80,num25=!random!%%80,num26=!random!%%80,num27=!random!%%80
  6. (
  7. set /a looptime+=1
  8. if not "%time:~6,2%"=="%sec%" (
  9.     set sec=%time:~6,2%&title  !looptime! loop/1sec"
  10.     set/a looptime=0
  11. )
  12.     set nub1=!a:~-%num1%,1!&set nub2=!a:~-%num2%,1!&set nub3=!a:~-%num3%,1!&set nub4=!a:~-%num4%,1!&set nub5=!a:~-%num5%,1!&set nub6=!a:~-%num6%,1!&set nub7=!a:~-%num7%,1!&set nub8=!a:~-%num8%,1!&set nub9=!a:~-%num9%,1!&set nub10=!a:~-%num10%,1!&set nub11=!a:~-%num11%,1!&set nub12=!a:~-%num12%,1!&set nub13=!a:~-%num13%,1!&set nub14=!a:~-%num14%,1!&set nub15=!a:~-%num15%,1!&set nub16=!a:~-%num16%,1!&set nub17=!a:~-%num17%,1!&set nub18=!a:~-%num18%,1!&set nub19=!a:~-%num19%,1!&set nub20=!a:~-%num20%,1!&set nub21=!a:~-%num21%,1!&set nub22=!a:~-%num22%,1!&set nub23=!a:~-%num23%,1!&set nub24=!a:~-%num24%,1!&set nub25=!a:~-%num25%,1!&set nub26=!a:~-%num26%,1!&set nub27=!a:~-%num27%,1!
  13.     set "c1=%nub1%  %nub2%  %nub3%  %nub4%  %nub5%  %nub6%  %nub7%  %nub8%  %nub9%  %nub10%  %nub11%  %nub12%  %nub13%  %nub14%  %nub15%  %nub16%  %nub17%  %nub18%  %nub19%  %nub20%  %nub21%  %nub22%  %nub23%  %nub24%  %nub25%  %nub26%  %nub27% "
  14. )>nul
  15. cls&set/p=!c1!!c2!!c3!!c4!!c5!!c6!!c7!!c8!!c9!!c10!!c11!!c12!!c13!!c14!!c15!!c16!!c17!!c18!!c19!!c20!!c21!!c22!!c23!!c24!!c25!<nul
  16. (
  17.     set c2=%c1%&set c3=%c2%&set c4=%c3%&set c5=%c4%&set c6=%c5%&set c7=%c6%&set c8=%c7%&set c9=%c8%&set c10=%c9%&set c11=%c10%&set c12=%c11%&set c13=%c12%&set c14=%c13%&set c15=%c14%&set c16=%c15%&set c17=%c16%&set c18=%c17%&set c19=%c18%&set c20=%c19%&set c21=%c20%&set c22=%c21%&set c23=%c22%&set c24=%c23%&set c25=%c24%
  18.     goto :loop
  19. )>nul
复制代码
"1.bat"程序巡環一次的過程跑最少63次for迥圈, 而且 ":change" 標籤 那里每一巡環也卡死差不多 50 至 60 次 //   總計程序巡環一次差不多跑120次迥圈 ;
"2.bat"程序巡環一次的過程跑約65次迥圈 ;
"3.bat"程序巡環中根本沒跑迥圈

總計效能 :
1.bat -  15 loop /1sec
2.bat -  80 loop /1sec
3.bat - 120 loop/1sec


結論是 , 根本不干cls & echo事 , 拖慢的原因是在於反覆計算次數越多畫面就越慢




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2