::递加日期,以处理5万行的文本为例
::本机测试 耗时: 0 小时 0 分钟 14 秒 72 毫秒
::这次测试怎么都要14秒。。郁闷!难道是电脑不同的原因?
代码效率关键是,for 内部运行的命令越少越好。
: | @echo off&setlocal enabledelayedexpansion | | set ttime=%time%&set wjm=ab cd.txt | | for /f "tokens=2 delims=:" %%a in ('find /v /c "" "%wjm%"') do set /a tian=%%a-1 | | call :Date2Day "%date:~0,10%" %tian% sy sm sd | | echo %tian% 天前的日期是:%sy%年%sm%月%sd%日 | | set /a sm+=100,sd+=100,n=100,yue113=131 | | for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do set /a n+=1&set /a yue!n!=1%%a | | set /a yue102="^!(sy%%4)&^!(^!(sy%%100))|^!(sy%%400)+128",py=yue!sm! | | echo.1>nul 3>c.txt | | for /f "usebackq delims=" %%a in ("%wjm%") do ( | | echo !sy!年!sm:~-2!月!sd:~-2!日 %%a | | set /a sd+=1 | | if !sd! gtr !py! ( | | set /a sd=101,sm+=1&set /a py=yue!sm!&echo. | | if !sm! gtr 112 ( | | set /a sy+=1,sm=101,yue102="^!(sy%%4)&^!(^!(sy%%100))|^!(sy%%400)+128" | | ))) | | | | echo.1>nul 4>con | | call :time0 "%ttime%" "%time%" ok | | echo 耗时: %ok%&title ok | | echo.&pause | | start notepad "c.txt"&exit | | | | :Date2Day 获取指定天数的日期 (封装) | | setlocal&rem by Will Sort @cn-dos | | for /f "tokens=1-3 delims=/-, " %%a in ('echo/%~1') do ( | | set /a yy=%%a,mm=100%%b%%100,dd=100%%c%%100) | | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2 | | set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633 | | set /a i=j-%~2,a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a | | set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5 | | set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10 | | endlocal&set %3=%yy%&set %4=%mm%&set %5=%dd%&goto :EOF | | | | :time0 计算批处理运行时间(封装) | | setlocal&set /a n=0&rem by 随风 @bbs.bathome.net | | for /f "tokens=1-8 delims=.: " %%a in ("%~1:%~2") do ( | | set /a n+=10%%a%%100*360000+10%%b%%100*6000+10%%c%%100*100+10%%d%%100 | | set /a n-=10%%e%%100*360000+10%%f%%100*6000+10%%g%%100*100+10%%h%%100) | | set /a s=n/360000,n=n%%360000,f=n/6000,n=n%%6000,m=n/100,n=n%%100 | | set "ok=%s% 小时 %f% 分钟 %m% 秒 %n% 毫秒" | | endlocal&set "%~3=%ok:-=%"&goto :eofCOPY |
[ 本帖最后由 随风 于 2008-10-20 17:04 编辑 ] |