Nice work Carlos.
I took your idea and optimized it some more. I was able to basically double the speed yet again
1) There is no point in creating more processes than you have processors (or cores)
2) Better to launch the last process synchronously. Then you don't have to begin checking if done until the last one completes. Each process is doing basically the same amount of work, so they should all end about the same time. So I don't bother putting a delay in the wait loop since I am confident it won't be running for long.
The above points don't really improve speed, but they do minimize the number of processes that the OS has to keep track of.
3) Most of the printable ASCII characters can be generated much more quickly using CMD /C EXIT n to set %=exitCodeAscii%. This saves a lot of time.
Code: Select all
@echo off
setlocal
:genAllChr
::This code creates 256 1 byte files, one for each possible byte value.
::This is encoded in a macro way to be called asynchronously with start cmd /c
::Teamwork of carlos, penpen, aGerman, dbenham
::Tested under Win7 and XP
set ^"genchr=(^
for /l %%N in (%%A !cnt! 255) do (^
if %%N equ 26 (^
copy /y nul + nul /a 26.chr /a ^>nul^
) else (if %%N geq 35 if %%N leq 126 if %%N neq 61 (^
cmd /c exit %%N^&^
^<nul set /p "=!=exitCodeAscii!" ^>%%N.chr^
))^&^
if not exist %%N.chr (^
makecab /d compress=off /d reserveperdatablocksize=26 /d reserveperfoldersize=%%N %%A.tmp %%N.chr ^>nul^&^
type %%N.chr ^| ((for /l %%n in (1 1 38) do pause)^>nul^&findstr "^^" ^>%%N.temp)^&^
^>nul copy /y %%N.temp /a %%N.chr /b^&^
del %%N.temp^
)^
))^&^
del %%A.tmp^"
del /f /q /a *.chr *.tmp >nul 2>&1
set /a cnt=number_of_processors
if %cnt% lss 1 set cnt=1
if %cnt% gtr 256 set cnt=256
set /a "end=cnt-1"
for /l %%A in (0 1 %end%) do (
type nul >%%A.tmp
if %%A equ %end% (
start "" /b /wait cmd /q /v:on /c "%genchr%"
) else (
start "" /b cmd /q /v:on /c "%genchr%"
)
)
:genAllChr.check
if exist *.tmp goto :genAllChr.check
exit /b
Here are some timings I got comparing your original code and my modified version.
Code: Select all
Carlos' original My modifid version
------------------- --------------------
Win7 64bit 7.93 7.47 7.12 3.77 3.72 3.79
4 processors (cores) Avg = 7.51 Avg = 3.76
XP virtual machine 57.73 57.92 57.50 35.71 35.93 36.13
1 processor Avg = 57.72 Avg = 35.92
Dave Benham