StopWatch
- Namespace
- ZAux
- Implements
FUNCTION_BLOCK StopWatch IMPLEMENTS ZCore.IStopWatch
Simple clockrate-based stopwatch to measure time from one point in program to another one. It can be used for profiling the execution time of an algorithm and uses the CPU-Tick Count as lowest tick.
This implementations signature is based on MATLAB's way of measuring intervals. After instantiating the function block, the Tic method can be called to start the stopwatch. At any point Toc can be called to return the duration as LREAL. The duration is returned in the unit that is given as a parameter to the Toc method. The duration since starting the timer may be evaluated multiple times, only calling Tic resets the new base for the toc method of the stopwatch.
Note
The Stopwatch internally uses of the CPUs internal clock rate. While this is great for profiling, keep in mind that during breakpoints (used when debugging a PLC), the time keeps running and therefore, the duration increases. Also, since CPUs are not atomic clocks, the duration may differ from the actual duration by a bit.
Example
_stopwatch : ZAux.StopWatch;
_duration : LREAL;
---
_stopwatch.Tic();
ResourceHungryEvaluation();
_duration := _stopwatch.Toc(TimeUnit.Second);
Properties
Started
PROPERTY Started : BOOL
Property Value
- BOOL
Methods
Stop
METHOD Stop ()
Resets the stopwatch if started. After calling this method, Started will return FALSE
.
This behavior is consistent with Timer.
Tic
METHOD Tic ()
Starts the time measurement by saving the actual CPU-Tick-Count
Toc
METHOD Toc (
[input] unit : ZCore.TimeUnit) : LREAL
Returns the time between Tic and Toc method calls in the desired unit. This method may be called multiple times to evaluate the elapsed duration since Tic was called. If Toc gets called before Tic or an unsupported TimeUnit is used as parameter, this method will return 0.
Inputs
unit
TimeUnit
Returns
- LREAL