Avoid Using Range Subscripts
It is possible to use range subscripts in an assignment statement, however, when possible, you should avoid using range subscripts in favor of using scalar or array subscripts. This type of assignment statement takes the following form:
A subscript range specifies a beginning and ending subscripts, which are separated by the colon character. An ending subscript equal to the size of the dimension minus one can be written as *. For example, arr[I:J] denotes those points in the vector arr with subscripts between I and J inclusive. I must be less than or equal to J and greater than or equal to zero. J denotes the points in arr from arr[I] to the last point and must be less than the size of the dimension arr [I:*]. See Subscript Ranges for more details on subscript ranges.
When possible, you should avoid using range subscripts in favor of using scalar or array subscripts. In the following example, the array elements of X are inserted into array A. The slow way uses subscript ranges, specifying the insertion of X array elements into the 5th through 7th elements of A. The fast way uses a scalar subscript specifying the first element (the 5th) to be replaced with the elements of A.
A = INTARR(10) X = [1,1,1] PRINT, 'A = ', A ; Slow way: t = SYSTIME(1) & FOR i=0L,100000 DO A[4:6] = X & PRINT,'Slow way: ', SYSTIME(1)-t PRINT, 'A = ', A ; Correct way is 4 times faster!!: t = SYSTIME(1) & FOR i=0L,100000 DO A[4] = X & PRINT, 'Fast way: ', SYSTIME(1)-t PRINT, 'A = ', A
IDL prints:
A = 0 0 0 0 0 0 0 0 0 0 Slow way: 0.47000003 A = 0 0 0 0 1 1 1 0 0 0 Fast way: 0.12100005 A = 0 0 0 0 1 1 1 0 0 0
The statement A[4] = X, where X is a three-element array, causes IDL to start at index 4 of array A, and replace the next three elements in A with the elements in X. Because of the way it is implemented in IDL, A[4] = X is much more efficient than A[4:6] = X.