Testing of PLC code execution time and influencing factors


Keep curiosity about technology, broaden the boundaries of knowledge, and improve the resolution of understanding
 CX2100 PLC  (Photo: beckhoff.com/manul)
  
Applicable scope:
During the program development process, it was found that the CPU utilization was too high, and it was hoped that the PLC code could be optimized to find out which PLC code was wasting time and whether it could be optimized. This is a problem that programmers need to consider. TwinCAT PLC's TcUtinity.lib provides a function block GETCPUCOUNTER, which can be called multiple times in the same PLC cycle to obtain the CPU time in real time.The difference between the two obtained times in the same cycle is the code execution time between the two FB calls.Using this function, not only can the code be optimized, but also the execution time of various PLC instructions can be compared quantitatively.It can also be used to analyze the impact of programs that affect CPU execution, such as TcpIp communication, ADS communication, Login, Display Flowcontrol, and other operations.




Three phenomena tested:

Analysis of instruction execution time

MEMCPY takes 3 times as long as a normal assignment statement.

The operation time for assignment, negation, and assignment, and judgment is comparable, and is the basic instruction time of PLC.

The execution speed of I7 virtual machine is about 4 times that of CX1020

Integer addition and subtraction is 2-2.5 times as fast as assignment statements

The integer multiplication and division operation is 1.5-2 times faster than the assignment statement

*Other operations: opening, reading, and writing files, which may take time, can be tested by the user themselves. The method is shown in the attachment.



The influence of Display Flow Control on CPU execution time

The currently highlighted code line will increase CPU execution time.Code lines that are not highlighted are not affected.

From SystemTaskInfoArr, it can be seen that after minimizing the window highlighting the code line, LastExecTime returns to normal.

The degree of impact: The impact of LAN connections is the greatest, and local virtual machines have a 10-fold smaller impact, although the response time using the ping command is <1ms.

Only Display Flow Control affects code execution, and the degree of influence is independent of the PLC cycle.



Analysis of the impact of Login on instruction execution time

Login to the program by PLC Control does not affect code execution.Only DisplayFlow Control does.

There is no difference in PLC execution time compared to directly logging in to 801 without IO mapping to view the results.

* CPU utilization may vary because it is based on IO processing time, not CPU operation time.



Applicable scope:
Three phenomena were tested:
Analysis tool:
Test record
1. Analysis of instruction execution time
2. Impact of Display Flow Control on CPU execution time
3. Analysis of the impact of Login on instruction execution time