#include <Arduino.h>
const int N = 100; // Size of the array
bool boolArray[N];
unsigned char bitArray[(N + 7) / 8] = {};
// Function declarations
void readBoolArray();
void writeBoolArray();
void readBitArray();
void writeBitArray();
void setup() {
Serial.begin(9600);
while (!Serial); // Wait for Serial port to connect
// Measure and report the speed of reading from a bool array
unsigned long start = micros();
readBoolArray();
unsigned long duration = micros() - start;
Serial.print("Reading Bool Array took: ");
Serial.print(duration);
Serial.println(" microseconds");
// Measure and report the speed of writing to a bool array
start = micros();
writeBoolArray();
duration = micros() - start;
Serial.print("Writing Bool Array took: ");
Serial.print(duration);
Serial.println(" microseconds");
// Measure and report the speed of reading bits from a bit array
start = micros();
readBitArray();
duration = micros() - start;
Serial.print("Reading Bit Array took: ");
Serial.print(duration);
Serial.println(" microseconds");
// Measure and report the speed of writing bits to a bit array
start = micros();
writeBitArray();
duration = micros() - start;
Serial.print("Writing Bit Array took: ");
Serial.print(duration);
Serial.println(" microseconds");
}
void loop() {
// Empty loop
}
void readBoolArray() {
for (int i = 0; i < N; i++) {
volatile bool readValue = boolArray[i];
}
}
void writeBoolArray() {
for (int i = 0; i < N; i++) {
boolArray[i] = (i % 2) == 0;
}
}
void readBitArray() {
for (int i = 0; i < N; i++) {
int byteIndex = i / 8;
int bitIndex = i % 8;
volatile bool readValue = (bitArray[byteIndex] & (1 << bitIndex)) != 0;
}
}
void writeBitArray() {
for (int i = 0; i < N; i++) {
int byteIndex = i / 8;
int bitIndex = i % 8;
if ((i % 2) == 0) {
bitArray[byteIndex] |= (1 << bitIndex); // Set bit
} else {
bitArray[byteIndex] &= ~(1 << bitIndex); // Clear bit
}
}
}