paulsoulsby:
I'm trying to streamline the code in my loop subroutine as much as possible. It needs to run as quickly as possible and there's a monumental amount of code to process in each loop!
My current worry is about a few calls to the standard math exp function. Is this quite slow or should I not be worrying about it? I guess what I really want to know is how it compares with other math functions (add, divide, sqrt etc). If it's significantly slower, I may need to think of another way to do it (e.g. lookup tables).
Paul
All Arduinos (including the Due) have no direct support for floating point, and need to emulate it in software. So figure a basic add might be something like a hundred cycles, a multiply a couple of hundred, and divide and square root are measured in thousands of cycles. Exp is made up of quite a few multiplies, adds, and compares, so it is also fairly slow.
So, if you are extremely speed challenged, you need to figure how to do the calculations in integer arithmetic (and note on non-Due Arduinos, even integer calculations are multiple cycles, but it is still much faster than floating point).
Or alternatively, if you cannot do it fast enough on the Arduino, you need to move to a processor with a faster clock rate and that has floating point calculations in hardware, such as the Raspberry Pi, which has a clock rate 40 times the Arduino, much, much more memory, and has both 32-bit integer and floating point direct instructions. If you search around, you will find there are various melds between the Arduino to control devices, and the R-PI to do the brunt of the calculations (or eliminating the Arduino, and doing h/w control on the R-PI). I'm not sure whether the pcDunio offered by Sparkfun has hardware floating point, but even emulated floating point should be faster than Arduinos.
Note, be sure to actually measure whether exp is slowing down your app. Perhaps your bottleneck is waiting for some event, perhaps it is the speed of the floating point calculations.