Another good thread about optimized strlen functions is also at dostips strLen boosted
started by sowgtsoi.
One of the fastest methods to get the strlen is not a function, it's a macro.
Code: Select all
%$strlen% result myVar
::Above 2 blank lines are required - do not remove
:::: StrLen pResult pString
set $strLen=for /L %%n in (1 1 2) do if %%n==2 (%\n%
for /F "tokens=1,2 delims=, " %%1 in ("!argv!") do (%\n%
for /l %%A in (12,-1,0) do (%\n%
set /a "len|=1<<%%A"%\n%
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"%\n%
for %%v in (!len!) do endlocal^&if "%%~b" neq "" (set "%%~1=%%v") else echo %%v%\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=,
The macro technic is also explained somewhere here
But to your question
Sponge Belly wrote:Quote:
The first parenthesis blocks is only for a bit more performance.
Please expand on that remark. How does the presence of parentheses affect performance? Is there a rule of thumb for when a programmer should or should not enclose a block in parentheses?
A parenthesis block is mostly a bit faster, as it's completly parsed and cached before it's executed.
And to you last question
Sponge Belly wrote:
Lastly, is the above code the “definitive” version of StrLen? There are a lot of variants floating around and I’m not sure which one to use.
No, there isn't a definitive version, a good version for short strings can be slow for long strings, or another version depends on the disk speed.
But a good version should be able to handle long strings up to 8190 characters.
And it should be exact and independent for any possible character in the string.