I was doing some research into integer division on the Arduino Uno, and trying to determine the fastest methods of solving division.
In readings, I came across this post.
I grabbed and modified the sketch that Nick Gammon posted in reply #4, the ultimate aim being to compile a list of the computations that I need to perform, with their timings.
Here is my modified sketch:
#define DATATYPE byte
#define OPERATION i / j
volatile DATATYPE x=0;
volatile DATATYPE y=0;
//volatile DATATYPE j=7;
unsigned long time;
unsigned long loop_time;
void setup ()
{
Serial.begin(115200);
Serial.println ();
time = micros();
for (DATATYPE i = 1; i < 101; i++) {
//for (DATATYPE j = 1; j < 101; j++) {
for (DATATYPE j = 56; j < 57; j++) {
y++;
} // end of for j
} // end of for i
loop_time = micros() - time;
Serial.print("Loop baseline Completion time: ");
Serial.print(loop_time);
Serial.print(" ");
time = micros();
for (DATATYPE i = 1; i < 101; i++) {
//for (DATATYPE j = 1; j < 101; j++) {
for (DATATYPE j = 56; j < 57; j++) {
y++;
x = OPERATION;
} // end of for j
} // end of for i
time = micros() - time;
Serial.print("Completion time: ");
Serial.print(time);
Serial.print(" Operation time: ");
time = time - loop_time;
Serial.println(time);
} // end of setup
void loop() {}
The output of this is:
Loop baseline Completion time: 52 Completion time: 644 Operation time: 592
This essentially gives a time of 5usec for each division operation using bytes.
However, changing two loops from :
for (DATATYPE j = 56; j < 57; j++) {
to
for (DATATYPE j = 57; j < 58; j++) {
gives the following output:
Loop baseline Completion time: 52 Completion time: 108 Operation time: 56
I have tried a few different vales for the inner loop (7, 56, 58, 3) etc
3 gives a similar result, but 7, 11 and 13 do not
Can anyone tell me why there would be such a big difference in the times taken to perform the operation?