FAQ-1135 Why is speed slower when loop increases?

Last Update: 5/6/2022

Each loop is slower when number of loop increases if add columns and set designation take time when number of columns in the existing sheet is large:

//[Book1]1! already has 2450x3 columns
rw=2450;
sec;
loop (tt,1,50) 
{
ncol = rw*3;

//access non-existent column, so it will add column
range dis3=[Book1]1!col($(ncol+1));  
range vel3=[Book1]1!col($(ncol+2)); 
range vel3a=[Book1]1!col($(ncol+3));

dis3[L]$=f$(tt)dis;
vel3[L]$=f$(tt)vel;
vel3a[L]$=f$(tt)vela;

//set designation take time when number of columns in the existing sheet is large
dis3.type=4;
vel3.type=1;
vel3a.type=1;

rw=rw+1;
};

So we need to try to avoid above actions inside a loop, i.e. prepare the whole worksheet first before the loop:

int nloop = 50;

//specify the worksheet
range rWa = [Book4]1!;
int oldcol = rWa.nCols;

//add columns
rWa.nCols = rWa.nCols + nloop*3;

//set designations
coldesig desig:=X2Y rng:=%(rWa)$(oldcol+1):$(rWa.nCols);

loop (tt,1,nloop) 
{
range dis3=%(rWa)$(oldcol+3*tt-2);
range vel3=%(rWa)$(oldcol+3*tt-1);
range vel3a=%(rWa)$(oldcol+3*tt);

dis3[L]$=f$(tt)dis;
vel3[L]$=f$(tt)vel;
vel3a[L]$=f$(tt)vela;

}


Keywords:LabTalk, loop