给一段纯P代码,本想用js再优化一步其中的字符串长度计算代码,但实测结果,后者比前者竟然慢了...
纯P代码中的字符串长度计算算法源引自一个被认为P史以来非常经典的案例(参见 http://www.bathome.net/thread-4482-1-1.html)
以下代码均假设源文本文件与代码脚本在同一目录
一。纯P方案- @echo off
- set srcF="a.txt" &set newF="a.new"
- set/a "m=0","fsL=5" &::设置重复字段长度
- setlocal enabledelayedexpansion
- (for /f "usebackq delims=" %%s in (%srcF%) do (
- set "s=%%s" &(call :strLen s sL)
- set/a "n=!sL!-%fsL%","m+=1"
- for /L %%k in (0,1,!n!) do (
- set "sk=!s:~%%k,%fsL%!"
- (more +!m! %srcF%|findstr /i /c:"!sk!")>nul&&echo,!sk!
- )
- ))>%newF%
- endlocal&exit/b
-
- :strLen // %1为被测字符串变量名 %2为返回值变量名
- ( set "str=a!%~1!"
- set "len=0"
- for /l %%a in (12,-1,0) do (
- set/a "len|=1<<%%a"
- for %%b in (!len!) do if "!str:~%%b,1!"=="" set/a "len&=~1<<%%a"
- )
- )
- if "%~2" neq "" set/a "%~2=!len!"
- exit /b
复制代码 二。P+js混编方案- @set @v=1 /*
- @echo off
- set srcF="a.txt" &set newF="a.new"
- set/a "m=0","fsL=5" &::设置重复字段长度
- setlocal enabledelayedexpansion
- (for /f "usebackq delims=" %%s in (%srcF%) do (
- for /f "delims=" %%v in ('cscript /e:jscript "%~f0" "%%~s" ') do set "sL=%%v"
- set "s=%%s" &set/a "n=!sL!-%fsL%","m+=1"
- for /L %%k in (0,1,!n!) do (
- set "sk=!s:~%%k,%fsL%!"
- (more +!m! %srcF%|findstr /i /c:"!sk!")>nul&&echo,!sk!
- )
- ))>%newF%
- endlocal&exit/b
- */
- v=WSH.Arguments; //获取命令行参数
- WSH.Echo(v(0).length); //计算字符串长度
复制代码
|