// Code from How Fast Does Your Arduino Code Run?
// https://youtu.be/tnfeMCyLZSo,
// talofer99: port manipulation on arduino
void setup() { // void codeRunMeasured()
Serial.begin(115200);
pinMode(13, OUTPUT);
//** Set up Timer 1 to count every single clock cycle
TCCR1A = 0;
TCCR1B = bit(CS10); // or ( =1 << CS10;)
TCNT1 = 0;
/* Code under test */
//digitalWrite(13, HIGH); // slower 38cycles, 2,38us
//PORTB = bit(5); // faster 2cycles, 0,12us
//delayMicroseconds(501); // 7997cycles, 499,81us
//delayMicroseconds(52); // 813cycles, 50,81us
//delayMicroseconds(51); // 797cycles, 49,81us
delayMicroseconds(5); // 61cycles, 3,81us
//delayMicroseconds(5074); // 16013cycles, 1000,81us
//delay(5000); // 46119cycles, 2882,44us ????
//delay(50); // 13567cycles, 849,81us ????
/* End of code under test */
//** Print the result
unsigned long value = TCNT1;
Serial.print("Cycles: ");
Serial.println(value - 2);
Serial.print((float)(value - 2) / 16);
Serial.println(" usec.");
// If your frequency is 16 Mhz then (1/f = T) it means that
// 1 cycle takes 1/16 000 000 = 62.5 ns
// and if you have 38 cycles. 38 * 62.5 ns = 2375 ns = 2.38 micro seconds.
}
void loop() {
}