Microcontroller I/O & ADC Benchmarks

I had some time to go through, so I corrected my mistakes, with much greater detail.

  • I have made my own benchmark sketch, using parts from Riva's code (so some credit goes to Riva)
  • I have made a much more detailed file, which includes actual serial output
  • I tested 10, 5k, and 10k iterations
  • The file has the details at the top, then serial output at the bottom. It is very long so I labeled the line that each set of iterations begins on

The Table:

Board: Arduino 101 @ 32 MHz
IDE:   v1.6.9
Buad Rate of Serial: 115,200
Iterations per benchmark: 10 | 5,000 | 10,000

=====Digital Reading====

Iterations: 10
Digital Read of Pin 5 (total)  :         20.000 Microseconds 
Digital Read of Pin 5 (per)  :            2.000 Microseconds

Iterations: 5,000
Digital Read of Pin 5 (total)  :      8,920.000 Microseconds
Digital Read of Pin 5 (per)  :            1.784 Microseconds

Iterations: 10,000
Digital Read of Pin 5 (total)  :     17,836.000 Microseconds
Digital Read of Pin 5 (per)  :            1.784 Microseconds

Average Time per Read  :                  1.856 Microseconds

====Digital Writing====

Iterations: 10
Digital Write of Pin 5 (total)  :        18.500 Microseconds
Digital Write of Pin 5 (per)  :           1.850 Microseconds

Iterations: 5,000
Digital Write on Pin 5 (total) :      8,758.000 Microseconds
Digital Write on Pin 5 (per)  :           1.757 Microseconds

Iterations: 10,000
Digital Write on Pin 5 (total)  :    17,514.000 Microseconds
Digital Write on Pin 5 (per)  :           1.751 Microseconds

Average Time per Write  :                 1.786 Microseconds

====Analog Reading====

Iterations: 10
Analog Read on Pin A0 (total)  :        273.000 Microseconds
Analog Read on Pin A0 (per)  :           27.300 Microseconds

Iterations: 5,000
Analog Read on Pin A0 (total) :     140,082.000 Microseconds
Analog Read on Pin A0 (per)  :           28.016 Microseconds

Iterations: 10,000
Digital Read of Pin A0 (total)  :   280,365.000 Microseconds
Digital Read of Pin A0 (per)  :          28.036 Microseconds

Average Time per Read  :                 27.784 Microseconds

====PWM Writing (un-adjusted)====

Iterations: 10
PWM Write of Pin 5 (total)  :            52.500 Microseconds
PWM Write of Pin 5 (per)  :               5.250 Microseconds

Iterations: 5,000
PWM Write on Pin 5 (total)  :        23,805.000 Microseconds
PWM Write on Pin 5 (per)  :               4.761 Microseconds

Iterations: 10,000
Digital Write on Pin 5 (total)  :    47,605.000 Microseconds
Digital Write on Pin 5 (per)  :           4.761 Microseconds

Average Time per Write  :                 4.924 Microseconds

====PWM Writing (adjusted)====

Iterations: 10
PWM Write of Pin 5 (total)  :            28.000 Microseconds
PWM Write of Pin 5 (per)  :               2.800 Microseconds

Iterations: 5,000
PWM Write on Pin 5 (total)  :        12,389.000 Microseconds
PWM Write on Pin 5 (per)  :               2.479 Microseconds

Iterations: 10,000
Digital Write on Pin 5 (total)  :    24,777.000 Microseconds
Digital Write on Pin 5 (per)  :           2.478 Microseconds

Average Time per Write  :                 2.586 Microseconds
   
====Empty Loop====

Iterations: 10
Empty Loop (total)  :                     2.000 Microseconds
Empty Loop (per)  :                       0.200 Microseconds

Iterations: 5,000
Empty Loop (total)  :                   471.000 Microseconds
Empty Loop (per)  :                       0.094 Microseconds

Iterations: 10,000
Empty Loop (total)  :                   938.500 Microseconds
Empty Loop (per)  :                       0.094 Microseconds

Average Time per Loop  :                  0.130 Microseconds

====Mapping 0-5000 to 0-255====

Iterations: 10
Mapping (total)  :                       24.000 Microseconds
Mapping (per)  :                          2.400 Microseconds

Iterations: 5,000
Mapping (total)  :                   11,415.000 Microseconds
Mapping (per)  :                          2.283 Microseconds

Iterations: 10,000
Mapping (total)  :                   22,828.000 Microseconds
Mapping (per)  :                          2.282 Microseconds

Average Time per Map  :                   2.322 Microseconds

The code to get the information:

const uint16_t ITERATIONS = 10000;
const uint8_t DIGITAL_PIN = 5;
const uint8_t ANALOG_PIN = A0;

uint32_t startTime, totalTime;
uint16_t loopCount, d_read, a_read, map_test;

const uint16_t SESSION_LENGTH = 401; 
uint16_t cycleCount = 0;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  pinMode(ANALOG_PIN, INPUT);
}

void loop() {
  for (int x = 0; x < SESSION_LENGTH; x++)
  {
    TEST_D_READ();
    TEST_D_WRITE();
    TEST_A_READ();
    TEST_A_WRITE();
    TEST_LOOP();
    TEST_MAP();
    
    //delay(500);
    
    cycleCount++;
    Serial.print("Cycle: ");
    Serial.println(cycleCount);    
    Serial.println();
  }
  
  for(;;)
  {}

  
}

void TEST_D_READ() {
  startTime = micros();
  
  for (loopCount = 0; loopCount < ITERATIONS; loopCount++) {
    d_read = digitalRead(DIGITAL_PIN);
  }
  
  totalTime = micros() - startTime;

  Serial.print("Digital Read: ");
  Serial.print((float)totalTime, 4);
  Serial.println(" Microseconds");
  Serial.flush();
}

void TEST_D_WRITE() {
  startTime = micros();
  
  for (loopCount = 0; loopCount < ITERATIONS; loopCount++) {
    digitalWrite(DIGITAL_PIN, HIGH);
    digitalWrite(DIGITAL_PIN, LOW);
  }
  
  totalTime = micros() - startTime;

  Serial.print("Digital Write: ");
  Serial.print((float)totalTime / 2.0, 4);
  Serial.println(" Microseconds");
  Serial.flush();  
}

void TEST_A_READ() {
  startTime = micros();
  
  for (loopCount = 0; loopCount < ITERATIONS; loopCount++) {
    a_read = analogRead(ANALOG_PIN);
  }
  
  totalTime = micros() - startTime;

  Serial.print("Analog Read: ");
  Serial.print((float)totalTime, 4);
  Serial.println(" Microseconds");  
  Serial.flush();  
}

void TEST_A_WRITE() {
  startTime = micros();
  
  for (loopCount = 0; loopCount < ITERATIONS; loopCount++) {
    analogWrite(DIGITAL_PIN, map(loopCount, 0, ITERATIONS, 0, 255));
  }
  
  totalTime = micros() - startTime;

  Serial.print("PWM Write (not adjusted for mapping time): ");
  Serial.print((float)totalTime, 4);
  Serial.println(" Microseconds");
  Serial.flush();  
}

void TEST_LOOP() {
  startTime = micros();
  
  for (loopCount = 0; loopCount < ITERATIONS; loopCount++) {
    
  }
  
  totalTime = micros() - startTime;

  Serial.print("Empty Loop: ");
  Serial.print((float)totalTime, 4);
  Serial.println(" Microseconds");
  Serial.flush();  
}

void TEST_MAP() {
  startTime = micros();
  
  for (loopCount = 0; loopCount < ITERATIONS; loopCount++) {
    map_test = map(loopCount, 0, ITERATIONS, 0, 255);
  }
  
  totalTime = micros() - startTime;

  Serial.print("Mapping: ");
  Serial.print((float)totalTime, 4);
  Serial.println(" Microseconds");
  Serial.flush();  
}

Arduino 101 Proper Benchmark.txt (314 KB)

_101_Benchmark.ino (2.6 KB)