Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
alexperience
- Posts: 6
- Joined: 31 Jul 2012 10:22
#1
Post
by alexperience » 31 Jul 2012 10:27
Hi all, thanks for your patience. Is there a way to optimize this code for faster performance?
Thanks again,
Alex
Update: turns out the :END doesn't work anyways, none of the number counts are persisting to the :END ... Could you help with that too? lol turns out I know less then I thought when it comes to batch files.
Thanks again

Code: Select all
@ECHO OFF
SET /a x0=0
SET /a x1=0
SET /a x2=0
SET /a x3=0
SET /a x4=0
SET /a x5=0
SET /a x6=0
SET /a x7=0
SET /a x8=0
SET /a x9=0
SET /a x10=0
:START
SET /a rnd1=%random%%%10
SET /a rnd2=%random%%%10
SET /a rnd3=%random%%%10
SET /a rnd4=%random%%%10
SET /a rnd5=%random%%%10
SET /a rnd6=%random%%%10
SET /a counter+=1
if %counter% gtr 10000 GOTO :END
if %rnd1% == 0 SET x0+=1
if %rnd1% == 1 SET x1+=1
if %rnd1% == 2 SET x2+=1
if %rnd1% == 3 SET x3+=1
if %rnd1% == 4 SET x4+=1
if %rnd1% == 5 SET x5+=1
if %rnd1% == 6 SET x6+=1
if %rnd1% == 7 SET x7+=1
if %rnd1% == 8 SET x8+=1
if %rnd1% == 9 SET x9+=1
if %rnd1% == 10 SET x10+=1
if %rnd2% == 0 SET x0+=1
if %rnd2% == 1 SET x1+=1
if %rnd2% == 2 SET x2+=1
if %rnd2% == 3 SET x3+=1
if %rnd2% == 4 SET x4+=1
if %rnd2% == 5 SET x5+=1
if %rnd2% == 6 SET x6+=1
if %rnd2% == 7 SET x7+=1
if %rnd2% == 8 SET x8+=1
if %rnd2% == 9 SET x9+=1
if %rnd2% == 10 SET x10+=1
if %rnd3% == 0 SET x0+=1
if %rnd3% == 1 SET x1+=1
if %rnd3% == 2 SET x2+=1
if %rnd3% == 3 SET x3+=1
if %rnd3% == 4 SET x4+=1
if %rnd3% == 5 SET x5+=1
if %rnd3% == 6 SET x6+=1
if %rnd3% == 7 SET x7+=1
if %rnd3% == 8 SET x8+=1
if %rnd3% == 9 SET x9+=1
if %rnd3% == 10 SET x10+=1
if %rnd4% == 0 SET x0+=1
if %rnd4% == 1 SET x1+=1
if %rnd4% == 2 SET x2+=1
if %rnd4% == 3 SET x3+=1
if %rnd4% == 4 SET x4+=1
if %rnd4% == 5 SET x5+=1
if %rnd4% == 6 SET x6+=1
if %rnd4% == 7 SET x7+=1
if %rnd4% == 8 SET x8+=1
if %rnd4% == 9 SET x9+=1
if %rnd4% == 10 SET x10+=1
if %rnd5% == 0 SET x0+=1
if %rnd5% == 1 SET x1+=1
if %rnd5% == 2 SET x2+=1
if %rnd5% == 3 SET x3+=1
if %rnd5% == 4 SET x4+=1
if %rnd5% == 5 SET x5+=1
if %rnd5% == 6 SET x6+=1
if %rnd5% == 7 SET x7+=1
if %rnd5% == 8 SET x8+=1
if %rnd5% == 9 SET x9+=1
if %rnd5% == 10 SET x10+=1
if %rnd6% == 0 SET x0+=1
if %rnd6% == 1 SET x1+=1
if %rnd6% == 2 SET x2+=1
if %rnd6% == 3 SET x3+=1
if %rnd6% == 4 SET x4+=1
if %rnd6% == 5 SET x5+=1
if %rnd6% == 6 SET x6+=1
if %rnd6% == 7 SET x7+=1
if %rnd6% == 8 SET x8+=1
if %rnd6% == 9 SET x9+=1
if %rnd6% == 10 SET x10+=1
GOTO :START
:END
ECHO 0 = %x0%
ECHO 1 = %x1%
ECHO 2 = %x2%
ECHO 3 = %x3%
ECHO 4 = %x4%
ECHO 5 = %x5%
ECHO 6 = %x6%
ECHO 7 = %x7%
ECHO 8 = %x8%
ECHO 9 = %x9%
ECHO 10 = %x10%
PAUSE
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#2
Post
by abc0502 » 31 Jul 2012 10:52
hi, you are setting the rand variable wronge, i think you are using %% to get the result without the two tenths but dos already doesn't give you that, try this in dos
the result will be 3
if you want to get the remaining number u use the % so it will be 3 and the rest is 1
i don't know if u understand that, sorry for my english, not so good in math terms.
-
alexperience
- Posts: 6
- Joined: 31 Jul 2012 10:22
#3
Post
by alexperience » 31 Jul 2012 11:10
Thanks for your suggestion. the random number is working good. I have the code working now. I am just curious if it can be optimized. I runs really slowly lol Here is what I have now.
Code: Select all
@ECHO OFF
SET /a x0=0
SET /a x1=0
SET /a x2=0
SET /a x3=0
SET /a x4=0
SET /a x5=0
SET /a x6=0
SET /a x7=0
SET /a x8=0
SET /a x9=0
SET /a x10=0
:START
SET /a rnd1=%random%%%10
SET /a rnd2=%random%%%10
SET /a rnd3=%random%%%10
SET /a rnd4=%random%%%10
SET /a rnd5=%random%%%10
SET /a rnd6=%random%%%10
SET /a counter+=1
if %counter% gtr 10000 GOTO :END
if %rnd1% == 0 SET /a x0=%x0%+1
if %rnd1% == 1 SET /a x1=%x1%+1
if %rnd1% == 2 SET /a x2=%x2%+1
if %rnd1% == 3 SET /a x3=%x3%+1
if %rnd1% == 4 SET /a x4=%x4%+1
if %rnd1% == 5 SET /a x5=%x5%+1
if %rnd1% == 6 SET /a x6=%x6%+1
if %rnd1% == 7 SET /a x7=%x7%+1
if %rnd1% == 8 SET /a x8=%x8%+1
if %rnd1% == 9 SET /a x9=%x9%+1
if %rnd1% == 10 SET /a x10=%x10%+1
if %rnd2% == 0 SET /a x0=%x0%+1
if %rnd2% == 1 SET /a x1=%x1%+1
if %rnd2% == 2 SET /a x2=%x2%+1
if %rnd2% == 3 SET /a x3=%x3%+1
if %rnd2% == 4 SET /a x4=%x4%+1
if %rnd2% == 5 SET /a x5=%x5%+1
if %rnd2% == 6 SET /a x6=%x6%+1
if %rnd2% == 7 SET /a x7=%x7%+1
if %rnd2% == 8 SET /a x8=%x8%+1
if %rnd2% == 9 SET /a x9=%x9%+1
if %rnd2% == 10 SET /a x10=%x10%+1
if %rnd3% == 0 SET /a x0=%x0%+1
if %rnd3% == 1 SET /a x1=%x1%+1
if %rnd3% == 2 SET /a x2=%x2%+1
if %rnd3% == 3 SET /a x3=%x3%+1
if %rnd3% == 4 SET /a x4=%x4%+1
if %rnd3% == 5 SET /a x5=%x5%+1
if %rnd3% == 6 SET /a x6=%x6%+1
if %rnd3% == 7 SET /a x7=%x7%+1
if %rnd3% == 8 SET /a x8=%x8%+1
if %rnd3% == 9 SET /a x9=%x9%+1
if %rnd3% == 10 SET /a x10=%x10%+1
if %rnd4% == 0 SET /a x0=%x0%+1
if %rnd4% == 1 SET /a x1=%x1%+1
if %rnd4% == 2 SET /a x2=%x2%+1
if %rnd4% == 3 SET /a x3=%x3%+1
if %rnd4% == 4 SET /a x4=%x4%+1
if %rnd4% == 5 SET /a x5=%x5%+1
if %rnd4% == 6 SET /a x6=%x6%+1
if %rnd4% == 7 SET /a x7=%x7%+1
if %rnd4% == 8 SET /a x8=%x8%+1
if %rnd4% == 9 SET /a x9=%x9%+1
if %rnd4% == 10 SET /a x10=%x10%+1
if %rnd5% == 0 SET /a x0=%x0%+1
if %rnd5% == 1 SET /a x1=%x1%+1
if %rnd5% == 2 SET /a x2=%x2%+1
if %rnd5% == 3 SET /a x3=%x3%+1
if %rnd5% == 4 SET /a x4=%x4%+1
if %rnd5% == 5 SET /a x5=%x5%+1
if %rnd5% == 6 SET /a x6=%x6%+1
if %rnd5% == 7 SET /a x7=%x7%+1
if %rnd5% == 8 SET /a x8=%x8%+1
if %rnd5% == 9 SET /a x9=%x9%+1
if %rnd5% == 10 SET /a x10=%x10%+1
if %rnd6% == 0 SET /a x0=%x0%+1
if %rnd6% == 1 SET /a x1=%x1%+1
if %rnd6% == 2 SET /a x2=%x2%+1
if %rnd6% == 3 SET /a x3=%x3%+1
if %rnd6% == 4 SET /a x4=%x4%+1
if %rnd6% == 5 SET /a x5=%x5%+1
if %rnd6% == 6 SET /a x6=%x6%+1
if %rnd6% == 7 SET /a x7=%x7%+1
if %rnd6% == 8 SET /a x8=%x8%+1
if %rnd6% == 9 SET /a x9=%x9%+1
if %rnd6% == 10 SET /a x10=%x10%+1
GOTO :START
:END
ECHO 0 = %x0%
ECHO 1 = %x1%
ECHO 2 = %x2%
ECHO 3 = %x3%
ECHO 4 = %x4%
ECHO 5 = %x5%
ECHO 6 = %x6%
ECHO 7 = %x7%
ECHO 8 = %x8%
ECHO 9 = %x9%
ECHO 10 = %x10%
PAUSE
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#4
Post
by abc0502 » 31 Jul 2012 11:18
so you want it smaller and faster
-
alexperience
- Posts: 6
- Joined: 31 Jul 2012 10:22
#5
Post
by alexperience » 31 Jul 2012 11:30
Exactly, more maintainable, less code, faster performance.
I'm new to batch/dos and it's been a great learning experience so far just getting this little idea up and running, i was hoping I'd talk to the experts and learn something about optimizing all this code

Thanks for your reply by the way!
-
Liviu
- Expert
- Posts: 470
- Joined: 13 Jan 2012 21:24
#6
Post
by Liviu » 31 Jul 2012 11:46
Code: Select all
@echo off
setlocal enabledelayedexpansion
for /l %%c in (1,1,10) do set /a x%%c = 0
for /l %%c in (1,1,10000) do (
for /l %%r in (1,1,6) do (
set /a rnd=!random! %% 10
set /a x!rnd! += 1
)
)
for /l %%c in (1,1,10) do echo x%%c = !x%%c!
Liviu
P.S. In the original code you can get about 10% speedup just by noticing that rnd1 is a number between 0..9 so the "if %rnd1% == 10 SET /a x10=%x10%+1" lines are useless (and x10 always remains at 0).
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#7
Post
by abc0502 » 31 Jul 2012 11:49
wow , that is too small compared with what i was making

-
alexperience
- Posts: 6
- Joined: 31 Jul 2012 10:22
#8
Post
by alexperience » 31 Jul 2012 11:56
Liviu wrote:Code: Select all
@echo off
setlocal enabledelayedexpansion
for /l %%c in (1,1,10) do set /a x%%c = 0
for /l %%c in (1,1,10000) do (
for /l %%r in (1,1,6) do (
set /a rnd=!random! %% 10
set /a x!rnd! += 1
)
)
for /l %%c in (1,1,10) do echo x%%c = !x%%c!
Liviu
P.S. In the original code you can get about 10% speedup just by noticing that rnd1 is a number between 0..9 so the "if %rnd1% == 10 SET /a x10=%x10%+1" lines are useless (and x10 always remains at 0).
That's crazy Liviu!!!
I'm afraid I have no idea "why" this works, but I'm glad it does!!
Can you explain what for /l %%c in (1,1,10) does?
Thanks again, that's amazing!
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#10
Post
by foxidrive » 31 Jul 2012 12:01
This runs in 48 seconds. Your version took 2:38 on my machine.
Edit: Liviu wins at 5 seconds!!
Code: Select all
@echo off
setlocal enabledelayedexpansion
for %%a in (0 1 2 3 4 5 6 7 8 9 10) do (
set var=x%%a
set /a !var!=0
)
set counter=0
:START
set /a counter+=1
for %%a in (1 2 3 4 5 6) do (
set var=rnd%%a
set /a !var!=!random!%%10
)
for %%z in (1 2 3 4 5 6) do (
for %%a in (0 1 2 3 4 5 6 7 8 9 10) do (
set var=x%%a
if !rnd%%z! EQU %%a set /a !var!+=1
)
)
if %counter% lss 10000 GOTO :START
for %%a in (0 1 2 3 4 5 6 7 8 9 10) do (
set var=!x%%a!
echo x%%a = !var!
)
PAUSE
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#12
Post
by foxidrive » 31 Jul 2012 12:21
It allows you to evaluate variables within a loop, by using !var! instead of %var%
-
Liviu
- Expert
- Posts: 470
- Joined: 13 Jan 2012 21:24
#13
Post
by Liviu » 31 Jul 2012 12:43
@alexperience, you can add some echo's and pause's to see what the code does step by step. Also, I'd recommend the "for /?" and "set /?" help screens which explain the (basics of) number loops and enabledelayedexpansion.
@foxidrive, I bet that replacing the goto with a loop in your code would shave much of the time difference off.
Liviu
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#14
Post
by foxidrive » 31 Jul 2012 12:57
Liviu wrote:@foxidrive, I bet that replacing the goto with a loop in your code would shave much of the time difference off.
Down to around 34 seconds and with the extra 10th loop variable removed.
-
einstein1969
- Expert
- Posts: 976
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
#15
Post
by einstein1969 » 31 Jul 2012 13:16
with speedmeter (cycles/sec)
version of liviu is ultrafast
Code: Select all
@echo off
setlocal enabledelayedexpansion
for /l %%c in (1,1,10) do set /a x%%c = 0
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set t0=%time:,=%
for /l %%c in (1,1,10000) do (
for /l %%r in (1,1,6) do (
set /a rnd=!random! %% 10
set /a x!rnd! += 1
)
set /a rr+=1
if !rr! equ 100 (
set t1=!time:,=!
set /a rr=0,sc=1!t1:~6,4!-1!t0:~6,4!,m=1!t1:~3,2!-1!t0:~3,2!,md=sc+m*6000
if !md! equ 0 (set /a vel=0) else (set /a vel=%%c*100/md)
<nul set /p "=Counter:%%c !vel!/s !CR!" )
)
echo(
for /l %%c in (1,1,10) do echo x%%c = !x%%c!
if the %time% use "," separator for centsecond this work... This fail in the hour change (rare case)