Here’s a benchmark with the various flavors as suggested above (time in microseconds):
Benchmark Sketch
Ver: 1 Time: 7434924 Val: 41143476.00
Ver: 2 Time: 1183496 Val: 41143492.00
Ver: 3 Time: 694020 Val: 41143492.00
Ver: 4 Time: 756992 Val: 41143492.00
Ver 1: apow(x,4)+bpow(x,3)+cpow(x,2)+dx+e;
Ver 2: axxxx+bxxx+cxx+dx+e;
Ver 3: (((a*x+b)*x+c)*x+d)*x+e;
Ver 4: fma(fma(fma(fma(a,x,b),x,c),x,d),x,e);
Here’s a copy of the benchmark sketch:
void setup()
{
float r,x;
float a=1,b=2,c=3,d=4,e=5;
uint32_t t;
Serial.begin(57600);
Serial.println("Benchmark Sketch");
t=micros();
r=0.0;
x=1.0;
for (int i=0;i<10000;i++) {
//ax4+bx3+cx2+dx+e.
r+=a*pow(x,4)+b*pow(x,3)+c*pow(x,2)+d*x+e;
x+=0.001;
}
print_r(1,t,r);
t=micros();
r=0.0;
x=1.0;
for (int i=0;i<10000;i++) {
//(a*x*x*x*x)+(b*x*x*x)+(c*x*x)+(d*x)+e;
r+=a*x*x*x*x+b*x*x*x+c*x*x+d*x+e;
x+=0.001;
}
print_r(2,t,r);
t=micros();
r=0.0;
x=1.0;
for (int i=0;i<10000;i++) {
//(((a*x+b)*x+c)*x+d)*x+e
r+=(((a*x+b)*x+c)*x+d)*x+e;
x+=0.001;
}
print_r(3,t,r);
t=micros();
r=0.0;
x=1.0;
for (int i=0;i<10000;i++) {
//r=fma(fma(fma(fma(a,x,b),x,c),x,d),x,e)
r+=fma(fma(fma(fma(a,x,b),x,c),x,d),x,e);
x+=0.001;
}
print_r(4,t,r);
}
void print_r(byte ver, uint32_t time, float val)
{
Serial.print("Ver: ");
Serial.print(ver,DEC);
Serial.print(" Time: ");
Serial.print(micros()-time);
Serial.print(" Val: ");
Serial.println(val,2);
}
void loop()
{
}
Edit: I modified it to make sure that the main expression is recalculated every time through the loop. Results suggest that using pow() is indeed the slower version.