Initializing an array fast

May 20, 2008
10,675
83
Syracuse, NY, USA
I often do Montecarlo simulations and they almost always use an array. An array of binary flags ("a" == "" vs. "a" != "") can be initialized to empty strings very quickly with SETARRAY /F. For example, this takes 0.15 seconds.

Code:
do i=0 to 1000
    setarray /f a[100]
enddo

If an array will hold other data (counts, say) then they must be initialized to a value (to 0 say) and that takes a bit longer. This takes 15 seconds.

Code:
do i=0 to 1000
    do j=0 to 99 ( set a[%j]=0 )
enddo

That's 100 times slower. Please give us a fast way to initialize a one-dimensional array. A couple possibilities ... at SETARRAY time ...

Code:
SETARRAY /F /VALUE=0 a[100]

or sometime later ...

Code:
ECHO %@INITARRAY[a,0] > NUL