⚡️ Speed up function sensor_vals_to_temp by 13%
#24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 13% (0.13x) speedup for
sensor_vals_to_tempinopendm/thermal_tools/thermal_utils.py⏱️ Runtime :
1.39 milliseconds→1.23 milliseconds(best of129runs)📝 Explanation and details
The optimization achieves a 13% speedup by eliminating redundant expensive calculations:
Key optimizations:
Eliminated duplicate tau2 calculation: The original code computed the same complex exponential expression for both
tau1andtau2. The optimized version simply assignstau2 = tau1, saving ~150ms of computation time (5.2% → 0.5% of total runtime).Reused identical Planck radiance calculations:
raw_refl2now reusesraw_refl1instead of recalculating the same Planck equation with identicalReflectedApparentTemperatureraw_atm2now reusesraw_atm1instead of recalculating with the sameAtmosphericTemperatureReduced expensive numpy operations: These changes eliminate multiple calls to
np.exp()with complex expressions involvingPlanckBdivisions and temperature conversions.Performance impact by test case:
The optimizations are mathematically equivalent since
tau1andtau2use identical formulas, and the Planck radiance calculations use the same temperature inputs. The speedup comes from recognizing and exploiting these mathematical redundancies in the thermal sensor calibration equations.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-sensor_vals_to_temp-mh5gn58qand push.