ESP8266 loop is slow

Hi everyone,

I am new to the micro controller world. just started to learn.

I have 3 different controller boards : Arduino Mega 2560, ESP8266 (NodeMCU ESP12E), and ESP32-Dev Board.

Tested by a simple program to see how fast the loop of each CPU

the program is simple as below:

  1. define : volatile unsigned long loopcount = 0;
  2. setup a timer to print the loopcount each second
  3. inside the main loop: just loopcount++

Results:
1- Arduino Mega 2560 : around 311000/s (strangely, sometimes showing doubled the speed), about 1.5~3us/loop

2- ESP8266: 176000/s , 5.7 us/loop

3- ESP32: 993980 (very stable) , about 1us/loop

my question is - why ESP8266 is so slow ?

esp32.ino (492 Bytes)

mega.ino (574 Bytes)

esp8266.ino (256 Bytes)

Printing in an interrupt can be pretty tricky.

change your sketch to something like this (There is a warning but it compiles)

unsigned long loopcount = 0;
volatile bool flag = true; 

ISR(TIMER1_COMPA_vect) 
{
  flag = false;
}

void setup() 
{  
  // initialize timer1 
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;  
  TCCR1B = 0;  
  TCNT1  = 0;

  OCR1A = 31250 * 2;        // compare match register 16MHz/256/2Hz
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS12);    // 256 prescaler 
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();             // enable all interrupts
  Serial.begin(115200);
}

void loop() 
{
  while (flag) loopcount ++;
  // optionally insert code to stop timer
  Serial.println(loopcount);
  loopcount = 0;
  flag = true;
  // optionally insert code to start timer.
}

gives a quite steady output (on this side of the planet)

gr82morozr:
. . .

2- ESP8266: 176000/s , 5.7 us/loop

. . .

my question is - why ESP8266 is so slow ?

My understanding of Arduino on ESP8266 is that there is some cooperative multitasking in the implementation in the sense that WiFi status is checked and serviced if necessary on each iteration of "loop()". Thus there is significant overhead on your simple increment. If you had more computation in each loop of your "benchmark", the ESP8266 would appear relatively faster and ultimately considerably faster than the AtMega2560.

The ESP32 also has to periodically service WiFi, but it has more sophisticated thread tasking and two CPU cores, so loop() wouldn't be blocked by the WiFi task.

Dhrystone benchmarks show a different view.

http://www.esp8266.com/viewtopic.php?f=32&t=7438