Counting to 1000 In Both cores execution time problem in esp32? I am trying to count to 1000 with single core and dual core with esp32 but the output(run time) dose not make sense, the time which takes a single core should be doubled compared to dual core in theory right?
Here is the code
TaskHandle_t Task1;
TaskHandle_t Task2;
SemaphoreHandle_t baton;

int i = 0;
long l;

void setup() {

  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);

  baton = xSemaphoreCreateMutex();
  l = millis();
  xTaskCreatePinnedToCore(
    Task1code, /* Function to implement the task */
    &quot;Task1&quot;, /* Name of the task */
    10000,  /* Stack size in words */
    NULL,  /* Task input parameter */
    1,  /* Priority of the task */
    &amp;Task1,  /* Task handle. */
    0); /* Core where the task should run */

//single core or dual core execution

//  xTaskCreatePinnedToCore(
//    Task2code, /* Function to implement the task */
//    &quot;Task2&quot;, /* Name of the task */
//    10000,  /* Stack size in words */
//    NULL,  /* Task input parameter */
//    1,  /* Priority of the task */
//    &amp;Task2,  /* Task handle. */
//    1); /* Core where the task should run */

}
void Task1code( void * parameter) {
  while (true) {
    xSemaphoreTake(baton, portMAX_DELAY);
    xSemaphoreGive(baton);
    delay(1);// I don't understand in here without this command output result is worse
    i++;
    if (i == 1000) {
      l = millis()-1;
      Serial.print(&quot;Done &quot;);
      Serial.print(xPortGetCoreID());
      Serial.print(&quot; &quot;);
      Serial.println(l);
      
    }if(i&gt;1000){
      vTaskDelete(Task1);
    }
  }
}
void Task2code( void * parameter) {
  while (true) {
    xSemaphoreTake(baton, portMAX_DELAY);
    xSemaphoreGive(baton);
    delay(1);// I don't understand in here without this command output result is worse
    i++;
    if (i == 1000) {
      l = millis()-1;
      Serial.print(&quot;Done &quot;);
      Serial.print(xPortGetCoreID());
      Serial.print(&quot; &quot;);
      Serial.println(l);
    }if(i&gt;1000){
      vTaskDelete(Task2);
    }
  }
}
void loop() {

}

The output I get for runtime
Single core :- 1033 ms
Dual core :- 701 ms

Without delay(1); command the result is even more obvious
Single core :- 39 ms
Dual core :- 40 ms

How can I make my algorithm show that single core takes twice time as dual core execution time I wan't both cores to work parallel not one after the other.
Your must use the semaphore to protect the read/write access to i
(Only one task is allowed to modify the variable at a time)
xSemaphoreTake(baton, portMAX_DELAY);
i = i + 1;
xSemaphoreGive(baton);

Additionally you should combine your if clauses. If both tasks increment iit could happen, that it will never has the value 1000for the second task.