简体   繁体   English

降低python中的浮点精度以提高性能

[英]Lower the floating-point precision in python to increase performance

I'm working with python on raspberry pi. 我正在树莓派上使用python。 I'm using complementary filter to get better values from gyroscope, but it eats too much raspberry's power - it's about 70%. 我正在使用互补滤波器来从陀螺仪中获得更好的值,但是它消耗了覆盆子的过多功率-大约是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. 现在,结果大约有12位小数,远远超出了我的需要。 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! 编辑:我试图使用Decimal模块,并将精度设置为6,它比float慢了近6倍! 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. 您可以使用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. 但是,如果使用单精度浮点运算要快于双精度运算,我会感到非常惊讶:raspberry pi具有硬件浮点支持,因此我希望所有计算都以全80位精度完成,然后四舍五入为32位或保存到内存时为64位结果。 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. 它可以在慢速的8位处理器上实现,因此Raspberry太快了。

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. 如果您计算圆弧切线或等效函数,则将需要数百个FLOP。 If you do that at a rate of 1 kHz, you'll consume maybe a few hundred kFLOPS (FLoating-point OPerations per Second). 如果以1 kHz的速率执行此操作,则可能会消耗数百kFLOPS(每秒的浮点运算)。 The FP throughput of a RPi is approximately 100 MLFOPS, so there is a lot of margin. RPi的FP吞吐量约为100 MLFOPS,因此有很多余量。

Reducing the FP precision will thus not help significantly, the problem is elsewhere. 因此,降低FP精度将无济于事,问题出在其他地方。 Maybe if you show a bit more of your code, it could be determined where the problem is! 也许如果您显示更多代码,则可以确定问题出在哪里!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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