3-phase PV router
Loading...
Searching...
No Matches
bench_timer.h File Reference

Exact CPU cycle count of a benchmark loop, whatever its length. More...

#include <Arduino.h>
Include dependency graph for bench_timer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  BenchTimer
 

Functions

void BenchTimer::begin ()
 Starts counting: from the last instruction on, one count per CPU cycle.
 
uint32_t BenchTimer::end ()
 The cycles since begin(), its own instructions included.
 
 ISR (TIMER1_OVF_vect)
 
void BenchTimer::start ()
 Starts a measurement (the first one also measures the overhead).
 
uint32_t BenchTimer::stop ()
 The CPU cycles since start(), without those of start() and stop().
 

Variables

bool BenchTimer::calibrated { false }
 
volatile uint16_t BenchTimer::overflows { 0 }
 
uint32_t BenchTimer::overhead { 0 }
 
uint8_t BenchTimer::savedTimsk0 { 0 }
 

Detailed Description

Exact CPU cycle count of a benchmark loop, whatever its length.

Timer1 counts every CPU cycle; its overflow interrupt extends it to 32 bits, so a loop of up to 268 s can be timed. During the measurement, the millis() interrupt of Timer0 is paused and pending serial output has been sent: the only interrupt left is the Timer1 overflow, which happens a fixed number of times for a given loop. The count is then exact and the same from run to run, wherever the loop starts - unlike micros(), which advances in steps of 4 us and includes every millis() interrupt in between.

The cycles of start() and stop() themselves are measured once and subtracted.

Usage:

for (...) { ... }
const uint32_t cycles{ BenchTimer::stop() };
void start()
Starts a measurement (the first one also measures the overhead).
Definition bench_timer.h:72
uint32_t stop()
The CPU cycles since start(), without those of start() and stop().
Definition bench_timer.h:84
Note
Defines the Timer1 overflow interrupt: include it in one source file of a test only.

Definition in file bench_timer.h.

Function Documentation

◆ ISR()

ISR ( TIMER1_OVF_vect )

Definition at line 90 of file bench_timer.h.