# Creating a Time Budget

Last Modified: January 12, 2018

Minimize CPU usage and jitter in your application by creating a time budget for each task in your application. To create a time budget, you must determine the amount of time required to execute each task and set the rates of the loops accordingly.

1. Determine the required period of each task in your application.
2. Benchmark your application to determine the duration of each task in your application. Allow the benchmark to run for several thousand iterations and record the worst-case execution time as the duration.
3. Record the duration and period of each task in a time budget table, as shown in the following example.
Task Duration (µS) Period (µS)
Control 400 1,000
Monitor 3,000 10,000
Log 16,000 30,000
4. Use the following formula to target CPU usage at well below 100%: $\text{CPU Usage}\text{}\left(%\right)\text{\hspace{0.17em}}=\text{\hspace{0.17em}}100×\underset{1}{\overset{n}{\sum }}\left[{\mathrm{loop}}_{n}×\left(\frac{\mathrm{duration}}{\mathrm{period}}\right)\right]$. By plugging the numbers from the table into this formula, you can see that the CPU usage in this example is $100×\left(\frac{400}{1,000}+\frac{3,000}{10,000}+\frac{16,000}{30,000}\right)=123%$.
5. To decrease CPU usage, you can increase the period of one or more loops while ensuring that each loop executes at the requested rate. For example, by increasing the period of the monitoring loop to 25,000 µS and increasing the period of the logging loop to 80,000 µS, the theoretical CPU usage becomes $100×\left(\frac{400}{1,000}+\frac{3,000}{25,000}+\frac{16,000}{80,000}\right)=72%$.