strLen boosted

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4715
Joined: 22 Jan 2010 18:01
Location: Germany

Re: strLen boosted

#31 Post by aGerman » 06 Jul 2025 14:54

It's really unreadable, and the variable names are, at best, ugly.
Agreed. At least I tried to have it consistently ugly :lol:
However, it's tricky to get reliable time measurements on a modern system, where the CPU frequency fluctuates constantly.
Yeah. To mitigate it I run the tests in realtime priority. This makes sure we get always fed with enough resources. Without it I get results that are occasionally double as high as usual.

FWIW, that's how your code performs on my notebook if I add it to my test code.

Code: Select all

~~~~~~~~~~~~~~~~~~~~
jeb

8191
00:00:01.44
8191
00:00:01.49
8191
00:00:01.49
8191
00:00:01.49

1000
00:00:01.25
1000
00:00:01.28
1000
00:00:01.27
1000
00:00:01.27

200
00:00:01.11
200
00:00:01.15
200
00:00:01.14
200
00:00:01.16

10
00:00:01.11
10
00:00:01.16
10
00:00:01.14
10
00:00:01.14
Drücken Sie eine beliebige Taste . . .
Excluding the code for long strings makes processing short strings as quick as expected.

Code: Select all

%$lib.macrodefine.free% set strLen=for %%# in (1 2) do if %%#==2 ( %$\n%
    for /f "tokens=1,2" %%1 in ("%%!args%%!") do ( %$\n%
        set L=0 %$\n%
        if defined %%~1 ( %$\n%
            if "" neq "%%!%%~1:~255%%!" ( %$\n%
                for %%# in (4095 2047 1023 511 255) do ( %$\n%
                    set /a t=L+%%# %$\n%
                    for %%T in (%%!t%%!) do ( %$\n%
                        if "" neq "%%!%%~1:~%%T%%!" set /a L=%%T+1 %$\n%
                    ) %$\n%
                ) %$\n%
            ) %$\n%
            for %%# in (%%!L%%!) do ( %$\n%
                set ^"$=%%!%%~1:~%%#%%!^
FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210^
FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210^
FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210^
FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210^
FFFFFFFFFFFFFFFFEEEEEEEEEEEEEEEEDDDDDDDDDDDDDDDDCCCCCCCCCCCCCCCC^
BBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAA99999999999999998888888888888888^
7777777777777777666666666666666655555555555555554444444444444444^
333333333333333322222222222222221111111111111111^" %$\n%
                set /a L+=0x%%!$:~511,1%%!%%!$:~255,1%%! %$\n%
            ) %$\n%
        ) %$\n%
        for %%# in (%%!L%%!) do ( %$\n%
            endlocal %$\n%
            set %%~2=%%# %$\n%
        ) %$\n%
    ) %$\n%
 ) else setlocal EnableDelayedExpansion ^& set args=

Code: Select all

~~~~~~~~~~~~~~~~~~~~
jeb

8191
00:00:01.51
8191
00:00:01.55
8191
00:00:01.54
8191
00:00:01.53

1000
00:00:01.27
1000
00:00:01.30
1000
00:00:01.30
1000
00:00:01.30

200
00:00:00.69
200
00:00:00.72
200
00:00:00.70
200
00:00:00.71

10
00:00:00.69
10
00:00:00.71
10
00:00:00.72
10
00:00:00.71
Drücken Sie eine beliebige Taste . . .
Steffen

Post Reply