Skip to content

Instantly share code, notes, and snippets.

@jfjlaros
Last active January 23, 2021 12:46
Show Gist options
  • Save jfjlaros/fc25862ccdeb01fd972b5a42068e5cee to your computer and use it in GitHub Desktop.
Save jfjlaros/fc25862ccdeb01fd972b5a42068e5cee to your computer and use it in GitHub Desktop.
Snippet for timing analysis.
#define SLOTS 100
unsigned long _last[SLOTS];
size_t _index = 0;
void set(void) {
_last[_index] = millis();
_index++;
}
void delta(char* label) {
_index--;
char buf[80];
sprintf(buf, "%04i %s", millis() - _last[_index], label);
Serial.println(buf);
}
extern void set(void);
extern void delta(char*);
void some_function(void) {
set(); // Record current time in slot 0.
set(); // Record current time in slot 1.
some_call();
delta("some_call"); // Write the duration of `some_call()` to serial, free slot 1.
set(); // Record current time in slot 1.
some_other_call();
delta("some_other_call"); // Write the duration of `some_other_call()` to serial, free slot 1.
delta("total"); // Write the duration of the entire function to serial, free slot 0.
}
0247 some_call
0527 some_other_call
0774 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment