'Why does the execution speed of the while loop change over time?

We have a Time issue in a C while loop. in one part of the program, the while loop receives information from a sensor via the SPI protocol and stores it in a Mysql database, The problem appears when we are checking the stored data. For example, 600 data per second is stored in the database, but in some seconds the number of stored data is reduced to 400, and after a few seconds it returns to 600 per second, and so on. I know that the data transfer rate from the sensor works on a fixed clock and cannot be changed. But I do not know where this problem arises.

here is while loop code in c:

 while(1)
        {

            tx_buff[0] = 0x08 ;
            tx_buff[0] = (tx_buff[0] << 1) | 1;
            tx_buff[1] = 0xFF;
            tx_buff[2] = 0xFF;
            tx_buff[3] = 0xFF;
            tx_buff[4] = 0xFF;
            tx_buff[5] = 0xFF;
            tx_buff[6] = 0xFF;
            tx_buff[7] = 0xFF;
            tx_buff[8] = 0xFF;
            tx_buff[9] = 0xFF;
            tinkerboard_set_gpio_state(24, LOW);
            tinkerboard_spi_transfer(SPI2, tx_buff, rx_buff,10, mode);
            tinkerboard_set_gpio_state(24, HIGH);
            X_unsigned = (rx_buff[1]<<16) | (rx_buff[2]<<8)| (rx_buff[3]);
            X_unsigned = X_unsigned >> 4;
            X_signed = adc24to32(X_unsigned);
            Y_unsigned = (rx_buff[4]<<16) | (rx_buff[5]<<8)| (rx_buff[6]);
            Y_unsigned = Y_unsigned >> 4;
            Y_signed = adc24to32(Y_unsigned);
            Z_unsigned = (rx_buff[7]<<16) | (rx_buff[8]<<8)| (rx_buff[9]);
            Z_unsigned = Z_unsigned >> 4;
            Z_signed = adc24to32(Z_unsigned);

printf(" X=%5.4f Y=%5.4f Z=%5.4f \n",
X_signed*0.0039,Y_signed*0.0039,Z_signed*0.0039);
sprintf(query, "INSERT INTO PO(x,y,z) VALUES(%5.4f,%5.4f,%5.4f)",
X_signed*0.0039,Y_signed*0.0039,Z_signed*0.0039);

//conect to sql
     mysql_query(conn, query);
    
        }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source