Simulink Coder optimizes how counters are employed to measure absolute and elapsed time:

Time is computed from unsigned integer counters.

Only tasks that require time are allocated a counter.

Elapsed time is computed by a subsystem if and only if a block in its hierarchy requires elapsed time.

Time is shared by all blocks within a triggered hierarchy.

Simulink Coder optimizes counters based on the **Application lifespan** parameter on the **Optimization** pane. The number of bits used for a particular counter is optimized based on how long the application will run.

For example, assume an unsigned integer counter is incrementing at 1 kHz. The number of bits required to avoid an integer overflow is as follows:

Time span < 0.25 sec: 8-bits

Time span < 1 min: 16-bits

Time span < 49 days: 32-bits

Time span > 50 days: 64-bits

The 64-bit timer will not overflow for 590 million years!

The **Application lifespan** parameter allows you to avoid timer overflows efficiently without wasting memory.

```
open_system('rtwdemo_abstime')
```

In the `rtwdemo_abstime`

model, subsystem `SS1`

is clocked at 1 kHz, and contains a discrete-time integrator that requires elapsed time to compute its output. However, a counter is not required to compute elapsed time since the trigger port **Sample time type** is set to `periodic`

. Instead, time is inlined as 1 kHz.

Subsystem `SS2`

is clocked at 100 Hz, and contains a discrete-time integrator that requires elapsed time to compute its output. Since the **Application lifespan** of the model is 1 day, a 32-bit counter is required to compute elapsed time for `SS2`

.

Subsystem `SS3`

is clocked at 0.5 Hz, and contains a discrete-time integrator that requires elapsed time to compute its output. Since the **Application lifespan** of the model is 1 day, a 16-bit counter is required to compute elapsed time for `SS3`

.

```
bdclose('rtwdemo_abstime')
```

Was this topic helpful?