Speed of math operations (particularly division) on Arduino

Interesting, thanks. I did some tests which agree in general ways with the posted conclusions. Division is slower than multiplication. But you expect that, because how many people can do long division quickly? Compared to multiplication?

Sketch:

#define DATATYPE byte
#define OPERATION i / j

volatile DATATYPE x;
unsigned long time;
 
void setup ()
{
    Serial.begin(115200);
    Serial.println ();

    time = micros();
    for (DATATYPE i = 0; i < 100; i++) {
        for (DATATYPE j = 0; j < 100; j++) {
            x = OPERATION;
        } // end of for j
    }  // end of for i
    time = micros() - time;
    Serial.print("Completion time: ");
    Serial.println(time);
} // end of setup
 
void loop() {}

By varying the two defines (DATATYPE and OPERATION) I tested various combinations. I then subtracted out from the computed time 3800 uS which was the time taken for an operation of "1" (being the loop and a straight assignment).

Operation   Data type   Time uS   Time each uS  Clock cycles  x baseline
          
multiply    byte           640         0.064         1               1
multiply    int          3,808         0.3808        6               6
multiply    long         7,620         0.762        12              12
          
divide      byte       126,924        12.6924      203             198
divide      int        148,936        14.8936      238             233
divide      long       395,544        39.5544      633             618

The column "x baseline" compares to a straight byte multiply.

So certainly divides are slower. And using longer data types is slower.

If you know the value in advance it would obviously be faster to multiply by the inverse than divide.

2 Likes