strlen questions
Posted: 03 Apr 2013 05:42
Hi Jeb!
You answered this SO question in May, 2012. Your code for StrLen was as follows:
And then you said…
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?
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.
- SB
You answered this SO question in May, 2012. Your code for StrLen was as follows:
Code: Select all
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)
And then you said…
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?
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.

- SB