In data processing, WhileLoop() and DoLoop() functions certainly have their place; however, when a faster performance is required from code (drivers, on the fly data analysis, etc.), the VTS array processing functions are recommended (see Array Functions).
Consider the example below, which is seen quite frequently in code:
WhileLoop(I < N;
Array[I] = 0;
I++
)
This can be replaced by the simpler:
ArrayOp1(Array[0], N, 0, 0)
Although this is a simple example, imagine that this code were to execute every time a message were received from a PLC, and you had 50 PLC drivers running as quickly as possible – the result would be that a fair amount of processor time would be absorbed.
More obscure examples involve pre-formatting data received from an I/O device to read that data into arrays. Whenever possible, BuffToArray (and its sister BuffToParm) should be used for this task. In many situations, the data in these return messages is not in the correct format to use directly in the BuffToArray function, and it would appear at first glance that a loop must be used. The data can instead be pre-processed using an array function, and then passed to BuffToArray (an array processing function itself).
Consider the following example. Let's imagine that we are reading 4 byte HEX ASCII numbers from a device, but that the bytes are received in high to low order (the opposite to what the BuffToArray function expects), and that all data bytes representing 0 (0X30 HEX ASCII) have the highest order bit (b7) set to 1. The following fragment of code will read these values into an array correctly without requiring a loop.
RcvBuff = Replace(RcvBuff, 0, N * 4, MakeBuff(1, 0x30 + 0x80), MakeBuff(1,0x30));
RcvBuff = BuffOrder(RcvBuff, 0, 1, 4, N);
BuffToArray(Array[0], N, RcvBuff, 5, 5, 0);
Or to be even more compact:
BuffToArray(Array[0], N,
BuffOrder(Replace(RcvBuff, 0, N * 4,
MakeBuff(1, 0x30 + 0x80),
MakeBuff(1, 0x30)), 0, 1, 4, N),
5, 5, 0);
As a third, even faster option, you can remove the MakeBuff functions and replace them with text constants that are set only once.
Note: It is recommended that you consult the array functions section whenever you run up against a problem that will require some extensive looping to solve; chances are that the type of problem may have been encountered previously, and some array processing function(s) may already exist to help to solve the problem. The array functions are listed in Array Functions.