User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

[RESOLVED] pypy performance pants! Why?

Wed Jul 01, 2015 8:14 am

I need to improve the performance of my CPython (interpreted) code. I don't want to move to Cython due to the significant changes to my code it imposes. I tried pypy as its compiles to 'C' code just in time, and is supposed to make code run very much faster as a result. But it takes 17.1s (not including the JIT compile time) for my performance loop whereas the CPython takes 7.6 seconds.

This is the version of pypy which comes as part of the standard raspbian distro. According to the pypy web site, this version should be, on average, 6 times faster, but for me it's 2.25 times slower.

Any ideas why?

I have recompiled my GPIO and RPIO modules to run with pypy. The rest of my python code is using standard python libraries.

TIA
Last edited by Hove on Wed Jul 01, 2015 3:40 pm, edited 1 time in total.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
RaTTuS
Posts: 10563
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: pypy performance pants! Why?

Wed Jul 01, 2015 8:30 am

post your code to see what it is doing
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: pypy performance pants! Why?

Wed Jul 01, 2015 9:06 am

RaTTuS wrote:post your code to see what it is doing
Code is 2000 lines long so not really postable; it's up on GitHub at PiStuffing/Quadcopter/Quadcopter.py. To summarise it though it's structured like this:

GPIO.wait_for_edge() to detect hardware interrupt
smbus.i2c to read sensor data
various bits of basic trigonometry not using numpy
RPIO to produce a PWM output signal.

GPIO and RPIO are thin python wrappers around 'C' code, both of which I've recompiled / installed with 'sudo pypy ./setup.py install'

I have to assume it's one of GPIO, smbus or RPIO that are in some way incompatible, but that's purely speculative.

Does that give you further insight?
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pypy performance pants! Why?

Wed Jul 01, 2015 9:45 am

I wouldn't expect your code to be significantly faster, most of the heavy lifting is already being done by C, how much pure Python remains?

However I wouldn't expect it to be slower either.

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: pypy performance pants! Why?

Wed Jul 01, 2015 10:41 am

It's motion processing code that's taking the 3.5ms in addition to the <1ms for the sensor sampling.

Other than trigonometry, there is plenty of pure python arithmetic for
  • calculating angles
  • merging angles from accel and gyro
  • rotation matrix processing
  • butterworth filters (3 thereof)
  • PID processing (7 thereof )
Each is small, but together, I'd expect pypy to get this down to <<1ms which is what I need. So that fact it's gone up to 3.5ms from the 1.3ms I get from CPython is dissappointing
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: [RESOLVED] pypy performance pants! Why?

Wed Jul 01, 2015 3:46 pm

From the horse's mouth of the PyPy team: it's a miracle the PyPy works at all with the CPython C API used for GPIO / RPIO. The fact it does, and is only as slow as it is amazes them. The correct way for PyPy to call into ''C" code is to use a CFFI module: https://cffi.readthedocs.org/en/latest/overview.html will be much faster.

So there you have it!
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

Return to “Python”