WiFi.begin and WiFi.end Have Variable Execution Times

hello, so i have this code on arduino nano rp2040 Connect that tries to connect to a wifi to read the rssi then disconnect and goes for a second wifi to do the same, here the code :

currentMillis=millis();
             output = "";
        attemptConnection(SSID_WIFI1, PASSWORD_WIFI1, output);
           String tempOutput = "";
            attemptConnection(SSID_WIFI2, PASSWORD_WIFI2, tempOutput);
            output+=tempOutput;

        // Pad the response string with spaces until it reaches 10 characters
        while (output.length() < 15) {
            output += ' ';
        }
        command = '\0'; // Reset command after processing
      Serial.println(millis()-currentMillis);

as for the function attemptConnection :


void attemptConnection(const char* ssid, const char* password, String &chaine) {
    unsigned long startTime, endTime;

    
    if (WiFi.status() == WL_CONNECTED) {
        WiFi.disconnect();
        delay(100);
    }
    WiFi.begin(ssid, password);
    int attempts = 0; // Reset attempts counter

    while (WiFi.status() != WL_CONNECTED && attempts < MAX_wifi_try) {
        
        delay(DELAI_essaie);
        
        digitalWrite(LEDR, HIGH); // Turn on RED LED
        digitalWrite(LEDB, LOW); // Turn off BLUE LED
        attempts++;
    }
    if (WiFi.status() == WL_CONNECTED) {
        
        digitalWrite(LEDB, HIGH); // Turn on BLUE LED
        digitalWrite(LEDR, LOW); // Turn off RED LED    
        long rssi = WiFi.RSSI();
        chaine = String(rssi) + ",";
      
    } else {
        digitalWrite(LEDR, HIGH); // Turn on RED LED   
        digitalWrite(LEDB, LOW); // Turn off BLUE LED
        chaine = "KO,";  
    }
}

the delay between each try is 300 ms, and max try is set to 2 so in total 300 ms, after several tries i saw that the code block take times that varies from 3 second to 10 seconds ! which is too much , can anyone explain this variability ? mainly witnessing in Wifi.begin and Wifi.disconnect, i m doing something wrong in the code to note have consistent execution time ? and thanks in advance for help.

I think this could be normal. The Arduino must wait for responses from your router while it verifies passwords, allocates IP address etc and, during this time, other activity may be going on in your network making your router more or less responsive.

Too much for what?

You could try allocating a fixed IP address for the Arduino. That might reduce the connection time and the variability of the connection time.

WiFi.scanNetworks gives you rssi of all visible WiFi

thank you , i am conducting multiple measurements per position for various technologies. Waiting 10 seconds or more for a single WiFi measurement is excessive. Additionally, I want to minimize the time between successive WiFi measurements to reduce temporal variability, thereby making comparisons more consistent.

Why do you need to disconnect and reconnect to WiFi from same position?

A very good question, because there s more than one Wifi (not all boards give ability to play with a specific parameter, so need to play around with what i have).

again. use WiFi.scanNetworks