This time on measuring interesting code blocks, Ladder Vs FBD Vs ST Vs CFC. Which one is faster?

I made simple add two integer functions in each language codesys supports and the results were a bit confusing. SFC is not possible inside of functions so it was omitted from the measurements.

The expectation was that each of these IEC languages get similarly compiled to machine code and that they would have the same speed of execution. The compiled code would hold the same instructions to the PLC hardware.

So, I created functions for each language. This is to remove the function overhead from the measurements and also because my measurement framework is working on ST. I didn’t want to refactor my whole framework. It is not possible of course, to run all the languages under one program, unless they are in the functions.

The function is a simple a + b calculation, followed by and assignment to the function result. Then the value gets returned back to the calling program.

The results vary from machine to machine but these are the results I got.

Lang Time took (ms) Average across 5 runs(ms)
ST 3.959 - 5.228 4.816
LAD 5.26 - 7.59 6.184
FBD 3.9 - 4.7 4.256
CFC 3.9 - 4.7 4.256

When running these measurements many times the trend was that the Ladder was the slowest, ST second best, FBD and CFC identical run times, running the fastest.

Results Results


As always, the complete source code is on my github repository dedicated to speed testing.

Speedtest: Converting BOOL[1..32] to BYTE[1..4]

Speedtest: Calculations inside loop declarations

Speedtest: ST vs LADDER vs FBD vs CFC

Speedtest: IF-ELSIF vs CASE

Speedtest: Integer vs Floating point math