Python/Harmattan/Performance Considerations for Python Apps

Based on Python faster (for fmms initially) and Qt startup time tips

See the python.org page for general python optimizations.

Contents

Profiling

Do not worry about performance unless you notice a problem. Then only optimize what you can justify with profiling.

To profile Python code, run it with

$ python -m cProfile -o .profile TOPLEVEL_SCRIPT.py

To then analyze the results

$ python -m pstats .profile
> sort cumulative
> stats 40

That sorted the results by the time it took for a function and all the functions it called. It then displays the top 40 results.

See the python.org page for more information on profiling

Improving Performance

Interpreter Choice

Unladen Swallow

PEP 3146 - Merging of Unladen Swallow

Currently Unladen Swallow has not seen too much performance benefit but has a longer start up time and takes more memory

Psyco / Cython

Do these work with Arm?

Shedskin

C with CTypes

Startup

/usr/bin/python Startup

Preloaders exists like PyLauncher that keep a python process around with heavy weight imports like gtk already imported. On application launch it forks the preloader process.

Preloaders were favored back in the Maemo 4.1 days but has fallen out of favor lately. Concerns center around always keeping an unused python process with heavy pieces of code imported always around [1].

Parsing .py files

Stripping the Code

A major downside is that the code that your users is running is different than the code you develop with. This means any stack traces that users provide will be a bit more complicated to decipher.

Generating pyc/pyo files

Python serializes its state after importing a file to save on re-parsing. It saves these next to the .py files which means if the user does not have write access, Python will not be able to cache it.

Perceived Startup Performance

hildon_gtk_window_take_screenshot takes advantage of user perception to make the user think the app is launched faster.

Responsiveness

Worker Threads

The One Ring has separate threads for its DBus logic and its networking logic. it does this separation through a worker thread that the DBus thread posts tasks to. Results come as callbacks in the DBus thread.

See AsyncLinearExecutor and some example code

Memory Usage

FAQ

Is Python slow?

The standard response of "it depends". For a graphical application not doing too much processing a user will probably not notice it is written in Python. Compare that to an experiment by epage in writing a GST video filter in python that at best ran at 2 seconds per frame.