Skip to content

Instantly share code, notes, and snippets.

@igrr
Created April 13, 2016 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrr/3c57d465a300b74b42a3d937c8381be1 to your computer and use it in GitHub Desktop.
Save igrr/3c57d465a300b74b42a3d937c8381be1 to your computer and use it in GitHub Desktop.
#include <functional>
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("");
Serial.println("start");
}
uint32_t measure_time(std::function<void(void)> fn)
{
uint32_t t_start = micros();
fn();
uint32_t t_end = micros();
return t_end - t_start;
}
static uint32_t s_loop_end_timestamp = 0;
static uint32_t s_loop_counter = 0;
void loop() {
uint32_t loop_start_timestamp = micros();
uint32_t empty_loop_duration = measure_time( [](){
for (volatile int i = 0; i < 1000000; ++i) {}
});
uint32_t micros_loop_duration = measure_time( [](){
for (volatile int i = 0; i < 1000000; ++i) {
volatile int t = micros();
}
});
Serial.printf("Loop %4d t(empty loop)=%d t(micros loop)=%d t(loop end -> loop start)=%d\r\n",
s_loop_counter, empty_loop_duration, micros_loop_duration, loop_start_timestamp - s_loop_end_timestamp);
delay(100);
++s_loop_counter;
s_loop_end_timestamp = micros();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment