I am developing some engineering simulations. This involves implementing some long equations such as this equation to calculate stress in a rubber like material:
First thing to note is that pow
is really expensive, so you should get rid of this as much as possible. Scanning through the expression I see many repetitions of pow(l1 * l2 * l3, -0.1e1 / 0.3e1)
and pow(l1 * l2 * l3, -0.4e1 / 0.3e1)
. So I would expect a big gain from pre-computing those:
const double c1 = pow(l1 * l2 * l3, -0.1e1 / 0.3e1);
const double c2 = boost::math::pow<4>(c1);
where I am using the boost pow function.
Furthermore, you have some more pow
with exponent a
. If a
is Integer and known at compiler time, you can also replace those with boost::math::pow(...)
to gain further performance.
I would also suggest to replace terms like a / l1 / 0.3e1
with a / (l1 * 0.3e1)
as multiplication is faster then division.
Finally, if you use g++ you can use the -ffast-math
flag that allows the optimizer to be more aggressive in transforming equations. Read about what this flag actually does, as it has side effects though.