4

I would like to measure the execution time of a structured text (ST) program. The task associated with the program is running at 10 ms.

How do I measure execution time?

Roald
  • 2,459
  • 16
  • 43
Piper
  • 149
  • 11
  • 1
    Currently not very useful, but Beckhoff will introduce a Profiler product sometime in the future. I think it will come with 4026. It enables you to add a Profiler project to a PLC project. The Profiler will then show you execution times down to individual functions/function blocks. – Roald May 19 '21 at 13:32
  • Profiler was mentioned in the [2021 Automation update presentation](https://github.com/Roald87/TwinCatChangelog/files/6746267/___Beckhoff_automation-update-2021-twincat-e.pdf) – Roald Jul 01 '21 at 08:02

2 Answers2

5

You can use free TwinCAT library Tc2_Utilities that has a function block Profiler.

https://infosys.beckhoff.com/english.php?content=../content/1033/tcplclib_tc2_utilities/35053195.html&id=1344160655692967299

The "Profiler" function block can be used to allow the execution time of PLC code to be measured.

The Infosys page has an example code also:

VAR
    Profiler1     : PROFILER;
END_VAR


Profiler1(START := TRUE, RESET := TRUE);

//Do something here

Profiler1(START := FALSE);
//Now Profiler1.Data has the execution time
Quirzo
  • 1,183
  • 8
  • 10
1

Of course, you can use Profiler but just for demonstration purpose you can measure execution time like this.

PROGRAM PLC_PRG
    VAR
        tStart: TIME; (* Time program start *)
        tWork : TIME; (* Execution time *)
    END_VAR

    (* First line of main program *)
    tStart := TIME();

    // Your program here

    (* Last line of your program *)
    tWork := TIME() - tStart;
END_PROGRAM
Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38
  • This was my first thought. However my time difference is in ns and TIME() couldn't do measurement in that range. – Piper May 27 '21 at 08:05
  • Even with LTIME, which measures nano seconds, this doesn't seem to work. I think the TIME() and LTIME() functions return a value that is only updated once a frame. – Blue7 Aug 11 '23 at 13:14