Your question is Optimizing Code Performance. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
NASA test engineers replay recorded telemetry from static rocket engine fires to flag anomalies before the next test. This function runs on every new telemetry file, and on a full-duration burn (tens of thousands of samples) it is now slow enough that engineers are waiting on it between test stands.
def find_anomalies(readings, calibration_path="calibration.json"):
log = ""
duplicates = []
for i in range(len(readings)):
with open(calibration_path) as f:
calibration = json.load(f)
offset = calibration["temperature_offset"]
for j in range(len(readings)):
if i != j and readings[i]["timestamp"] == readings[j]["timestamp"]:
duplicates.append((i, j))
adjusted_temp = readings[i]["temperature"] + offset
if adjusted_temp > calibration["max_safe_temp"]:
log += "Anomaly at sample " + str(i) + ": " + str(adjusted_temp) + "
"
return log, duplicates
Explain exactly why this is slow on a large telemetry file and how you would rewrite it, in terms of what changes and roughly how much it should help, not just "make it faster."