简体   繁体   中英

Lower the floating-point precision in python to increase performance

I'm working with python on raspberry pi. I'm using complementary filter to get better values from gyroscope, but it eats too much raspberry's power - it's about 70%. I thought I could increase performance by reducing floating point precision. Now, results have about 12 decimal places, it's way more than I need. Is there any way to set maximum precision? Just rounding the number doesn't meet my needs, since it's just another calculation. Thanks!

Edit: I have tried to use Decimal module and with precision set to 6 it was nearly 6 times slower than float! Is there any other way to work with fixed-point numbers than Decimal (it looks to be created for higher precision than for performance)

You can force single precision floating point calculations using numpy.

However, I would be very surprised if using single precision floating point worked out any faster than double precision: the raspberry pi has hardware floating point support so I would expect that all calculations are done at full 80 bit precision and then rounded for 32 bit or 64 bit results when saving to memory. The only possible gain would be slightly less memory bandwidth used when saving the values.

It may be that you have the wrong end of the stick.

The data flow form a gyroscope is rather slow, so you should have ample time to filter it with any reasonable filter. Even a Kalman filter should be usable (though probably unnecessary). How often do you sample the gyroscope and accelerometer data? Reasonable maximum values are few hundred Hertz, not more.

The complementary filter for accelerometer and gyroscope measurement is very lightweight, and it by itself should consume very little processing power. It can be implemented on a slow 8-bit processor, so Raspberry is way too fast for it.

Depending on what you do with the complementary filter, the filter itself needs a few floating point operations. If you calculate arcus tangents or equivalent functions, that'll require hundreds of FLOPs. If you do that at a rate of 1 kHz, you'll consume maybe a few hundred kFLOPS (FLoating-point OPerations per Second). The FP throughput of a RPi is approximately 100 MLFOPS, so there is a lot of margin.

Reducing the FP precision will thus not help significantly, the problem is elsewhere. Maybe if you show a bit more of your code, it could be determined where the problem is!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM