Created
May 15, 2025 02:48
-
-
Save bdraco/cc4b758585b644cc3b757188b86d861d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api: | |
services: | |
- service: run_timer_benchmark | |
then: | |
- lambda: |- | |
ESP_LOGI("benchmark", "Running timer benchmark..."); | |
const int iterations = 100000; | |
volatile uint32_t dummy = 0; | |
struct Timer { | |
uint32_t cached_value; | |
uint32_t get() const { return cached_value; } | |
} timer; | |
// Warm-up | |
timer.cached_value = millis(); | |
dummy = timer.get(); | |
dummy = millis(); | |
// millis() timing | |
int64_t start = micros(); | |
for (int i = 0; i < iterations; ++i) { | |
dummy = millis(); | |
} | |
int64_t end = micros(); | |
double millis_us = (end - start) / static_cast<double>(iterations); | |
// Cached member access timing | |
timer.cached_value = 123456789; | |
start = micros(); | |
for (int i = 0; i < iterations; ++i) { | |
dummy = timer.get(); | |
} | |
end = micros(); | |
double cached_us = (end - start) / static_cast<double>(iterations); | |
ESP_LOGI("benchmark", "millis(): %.3f µs per call", millis_us); | |
ESP_LOGI("benchmark", "Cached class member access: %.6f µs per call", cached_us); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment