> For the complete documentation index, see [llms.txt](https://docs.cb21829.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cb21829.org/ftc-java-advanced-guide/loop-times-and-optimization.md).

# Loop times and optimization

Fast loops make controls feel tighter and more accurate.

### Why hardware calls matter

Every sensor read or motor write sends a command to the hub. These calls block and cost time.

The fix is simple:

* make fewer calls
* reuse values within the same loop
* use bulk reads

### Rule 1: never read the same hardware twice per loop

```java
int liftPos = lift.getCurrentPosition();
if (liftPos > 100) { ... }
telemetry.addData("pos", liftPos);
double p = pid.calculate(target, liftPos);
```

Read once. Reuse everywhere.

### Rule 2: use bulk reads

```java
import com.qualcomm.hardware.lynx.LynxModule;
import java.util.List;

List<LynxModule> allHubs = hardwareMap.getAll(LynxModule.class);
for (LynxModule hub : allHubs) {
    hub.setBulkCachingMode(LynxModule.BulkCachingMode.AUTO);
}
```

Bulk read modes:

* `OFF` — every call hits hardware
* `AUTO` — recommended default
* `MANUAL` — slightly faster, but easier to misuse

Manual mode looks like this:

```java
for (LynxModule hub : allHubs) hub.setBulkCachingMode(LynxModule.BulkCachingMode.MANUAL);

while (opModeIsActive()) {
    for (LynxModule hub : allHubs) hub.clearBulkCache();
}
```

### Measure your loop time

```java
ElapsedTime loopTimer = new ElapsedTime();
while (opModeIsActive()) {
    double loopMs = loopTimer.milliseconds();
    loopTimer.reset();
    telemetry.addData("loop (ms)", loopMs);
    telemetry.addData("hz", 1000.0 / loopMs);
    telemetry.update();
}
```

FTC Dashboard is the best tool for live tuning, plotting, and path visualization.

### Other wins

* avoid multithreaded hardware calls
* keep telemetry lean when chasing performance
* skip repeated motor writes if values did not change


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cb21829.org/ftc-java-advanced-guide/loop-times-and-optimization.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
