This source is based on GolamMostafa source. It shows that put method and multiple update methods have the same timing.
#include <EEPROM.h>
struct MyData
{
byte a;
byte b;
byte c;
byte d;
};
MyData data1;
MyData data2;
unsigned long t1, t2;
void EepromPut(int EEPROM_address, unsigned char *data, uint8_t dataLength)
{
int _address=EEPROM_address;
for (int i = 0; i < dataLength; i++)
{
EEPROM.update(_address, data[i]);
_address+=1;
}
}
void setup()
{
Serial.begin(9600);
while (!Serial);
Serial.println("EEPROM.put() Timing Test");
data1 = {10, 20, 30, 40}; //the values are in RAM
//-------- First Write (All bytes written) --------
t1 = micros();
EEPROM.put(0, data1); //10, 20, 30, 40 has entered into EEPROM
t2 = micros();
Serial.print("First write time (us) ");
Serial.println(t2 - t1);
//-------- Second Write (Same data, should skip writes) --------
t1 = micros();
EEPROM.put(0, data1);
t2 = micros();
Serial.print("Second write (no change) time (us) ");
Serial.println(t2 - t1);
//-------- Third Write (Only 1 byte should be written) --------
data1.c = 99; //-------- Modify ONE byte --------
t1 = micros();
EEPROM.put(0, data1);
t2 = micros();
Serial.print("Third write (1 byte changed) time (us) ");
Serial.println(t2 - t1);
//-------- Read back for verification --------
EEPROM.get(0, data2);
Serial.print("Read back ");
Serial.print(data2.a); Serial.print(", ");
Serial.print(data2.b); Serial.print(", ");
Serial.print(data2.c); Serial.print(", ");
Serial.println(data2.d);
//-------- Fourth Write (Only 2 bytes should be written) --------
data1.a=100; data1.b=200;
t1 = micros();
EepromPut(0, (unsigned char*)&data1, sizeof(data1)); //EEPROM.put(0, data1);
t2 = micros();
Serial.print("Fourth write (2 bytes changed) time (us) ");
Serial.println(t2 - t1);
//-------- Read back for verification --------
EEPROM.get(0, data2);
Serial.print("Read back ");
Serial.print(data2.a); Serial.print(", ");
Serial.print(data2.b); Serial.print(", ");
Serial.print(data2.c); Serial.print(", ");
Serial.println(data2.d);
Serial.println("");
Serial.println("********************************************************");
Serial.println("");
Serial.println("Multiple update() Timing Test");
data1 = {10, 20, 30, 40}; //the values are in RAM
//-------- First Write (All bytes written) --------
t1 = micros();
EepromPut(0, (unsigned char*)&data1, sizeof(data1)); //EEPROM.put(0, data1); 10, 20, 30, 40 has entered into EEPROM
t2 = micros();
Serial.print("First write time (us) ");
Serial.println(t2 - t1);
//-------- Second Write (Same data, should skip writes) --------
t1 = micros();
EepromPut(0, (unsigned char*)&data1, sizeof(data1)); //EEPROM.put(0, data1);
t2 = micros();
Serial.print("Second write (no change) time (us) ");
Serial.println(t2 - t1);
//-------- Third Write (Only 1 byte should be written) --------
data1.c = 99; //-------- Modify ONE byte --------
t1 = micros();
EepromPut(0, (unsigned char*)&data1, sizeof(data1)); //EEPROM.put(0, data1);
t2 = micros();
Serial.print("Third write (1 byte changed) time (us) ");
Serial.println(t2 - t1);
//-------- Read back for verification --------
EEPROM.get(0, data2);
Serial.print("Read back ");
Serial.print(data2.a); Serial.print(", ");
Serial.print(data2.b); Serial.print(", ");
Serial.print(data2.c); Serial.print(", ");
Serial.println(data2.d);
//-------- Fourth Write (Only 2 bytes should be written) --------
data1.a=100; data1.b=200;
t1 = micros();
EepromPut(0, (unsigned char*)&data1, sizeof(data1)); //EEPROM.put(0, data1);
t2 = micros();
Serial.print("Fourth write (2 bytes changed) time (us) ");
Serial.println(t2 - t1);
//-------- Read back for verification --------
EEPROM.get(0, data2);
Serial.print("Read back ");
Serial.print(data2.a); Serial.print(", ");
Serial.print(data2.b); Serial.print(", ");
Serial.print(data2.c); Serial.print(", ");
Serial.println(data2.d);
}
void loop() { }
First write time (us) 10376
Second write (no change) time (us) 16
Third write (1 byte changed) time (us) 3468
Read back 10, 20, 99, 40
Fourth write (2 bytes changed) time (us) 6920
Read back 100, 200, 99, 40
********************************************************
Multiple update() Timing Test
First write time (us) 10372
Second write (no change) time (us) 16
Third write (1 byte changed) time (us) 3468
Read back 10, 20, 99, 40
Fourth write (2 bytes changed) time (us) 6924
Read back 100, 200, 99, 40