![]() |
Making Python faster (for fmms initially)
Hi,
looking for suggestions/ideas/code/methods for making python faster! in #maemo irc, x-fade remembered about a pylauncher project, but does not know of its active status in fremantle: http://pylauncher.garage.maemo.org/ what other things can we do to effectively speed up python simple things, suggestions metrics etc does removing the comments make a noticable difference to script runtime? mini ad-hoc brainstorming session.. ================================================== ==== Update, added wiki page results A couple of weeks later and after much discussion and testing we have a fairly decent set of guidelines and best practices \o/ Performance_Considerations_for_Python_Apps thanks to epage for the excellent wiki writeup :) |
Re: Making Python faster (for fmms initially)
|
Re: Making Python faster (for fmms initially)
I'm not a developer so not 100% sure, but this thread is somewhat similar/hopefully helpful.
http://talk.maemo.org/showthread.php?t=47850 Edit: just noticed that the other thread is only for improving the inital startup times and not in general. My bad |
Re: Making Python faster (for fmms initially)
Quote:
|
Re: Making Python faster (for fmms initially)
Interesting post....... I feel i may be able to provide a few tests to back up issues....
Healthcheck currently has about 300 redundant lines of code (Comments + Blank lines) probably 25% of the total. I'll run a version of it without all of those and compare load times :D |
Re: Making Python faster (for fmms initially)
@lcuk
My damn N900 ate the smiley for some reason & made me look like a sad panda :p Anyways good to see the overall scheme includes start up improvements too. |
Re: Making Python faster (for fmms initially)
The import method of not importing everything has some postive impacts, though probably small.
- reduce startup time - reduces code size as your code no longer has to refer to the base class. ie qtcore.qrectangle becomes just qrectangle - not sure if references become a bit faster internally, but feels right to me. I would also use constants where possible rather than have them values recalculated, as is often the case in code. given most heavy lifting code is done inside the libraries like qt I,m not sure python is much slower, and if you ae not using libraries why not especially given the richness of them. of course YMMV |
Re: Making Python faster (for fmms initially)
from irc:
Quote:
|
Re: Making Python faster (for fmms initially)
When we talk about making Python "faster" do we mean:
(a) perceptionally faster (e.g. time to reach first screen) (b) execution speed |
Re: Making Python faster (for fmms initially)
Quote:
Also, for tricks using preloaders: Please consider what that does to memory - keeping GTK and/or QT bindings in there is quite a burden, think it through before you commit to such a thing. |
Re: Making Python faster (for fmms initially)
Quote:
its the same discussion at this point! :) |
Re: Making Python faster (for fmms initially)
Take it into the measurable realm by doing some benchmarking and profiling. When it comes to programming in general, picking the right algorithm that keeps down the complexity gives several magnitudes more effect than trying to micro optimize in lower layers.
When it comes to the Python run-time, check how it's compiled. What optimization is used? How will it affect what Python does the most (whatever that might be)? What's the aim? 1% faster? 10%? Depending on the goal, different methods might have to be combined. With a properly compiled Python runtime, good choice of algorithms and a reasonable memory footprint that doesn't cause too many cache misses, 1-5% might very well be possible. And don't forget the Linux environment. What scheduling is used in the kernels delivered by Nokia and the community? Has anyone looked into backporting the new ones, like BrainF u c k scheduling, that's specifically aimed at making the desktop faster? This might make the N900 as a whole faster. So, lots of variables. |
Re: Making Python faster (for fmms initially)
Far from a ready solution, but worth keeping an eye on (smells like Harmattan candidate material):
http://code.google.com/p/unladen-swa...ki/ProjectPlan |
Re: Making Python faster (for fmms initially)
use C++/Qt4 instead of PyQt
|
Re: Making Python faster (for fmms initially)
Quote:
That is as helpful as a chocolate matchstick! |
Re: Making Python faster (for fmms initially)
What we need are some benchmarks of how things are just now.
See where the bottlenecks are. Then start tweaking things in a controlled manner and compare the bechmarks to the original result. If we are talking about a specific program then we need to find what routines are the most intensive then maybe re-implement the routine in C/C++ an link it into the Pthon-based program. Profiling is probably the first step. Python profiling stuff |
Re: Making Python faster (for fmms initially)
Okay, not really a speed improvement. But the apple way to do the trick:
http://lists.maemo.org/pipermail/mae...er/021478.html :) |
Re: Making Python faster (for fmms initially)
Nouvelle note 47
Quote:
Please consider that a laucher use memory and in some case can be slower. Anyway ... there is simple things to optimize python scripts : - When available, prefer module methode instead of self made in python, as most of module are in c. So really faster - Avoid when possible too much search in namespace and prefer local method variables it s faster to assign a methode from namespace in a local var when it s called in a loop with more than 10 iterations - abuse of profile to see where your code is leaking time (available on device) - abuse of pylint - instead of doing many test case, use exception : a simple example : Code:
if (mydiv#0):Code:
try:- avoid cos , sin and tan in a game , use a precalculate array (for vectormine it s optimize fps from 3fps to 60fps) - and again abuse of profile - avoid converting data type in loop... Example QString to str - use existing module when available : PyGtKEditor do all his parsing in python ... it s can be slow on large file, khteditor do the things clearly faster by using some Qt native class. An other things to doesn't freeze the ui and to not slow down the device by using intensive thread is to divide tasks in very small tasks and to use a sigleton with gidle : Code:
import gobjectSo if you need so help optimizing code i can maybe help you. |
Re: Making Python faster (for fmms initially)
Khertan you are Sweenie Todd :D
I'm eating Py, that s**t is so advanced I dont even know what it does:D |
Re: Making Python faster (for fmms initially)
Quote:
GCDialer (old name for Dialcentral) use to remove extra whitespace and merge multiple python files into one. I didn't feel the cost/benefit was worth it but didn't benchmark it. I did do some benchmarking and found that class assignments are slow. When creating a lot of class variables (like pre-compiling a bunch of regexes) it was faster to create them on the instance. I normally have only one instance so no extra copies to worry about. For general python optimizations there is already a decent guide http://wiki.python.org/moin/PythonSpeed/PerformanceTips Quote:
http://www.python.org/dev/peps/pep-3146/ Long term, PyPy is the project I'm more excited about. They've recently added support for CPython extension modules which was a major blocker for transitioning. Speed is a mixed bag. I'm unsure about start time or memory usage though. http://speed.pypy.org/overview/ |
Re: Making Python faster (for fmms initially)
Quote:
AsyncLinearExecutor code: https://garage.maemo.org/plugins/ggi...51e98721401e72 Code that uses AsyncLinearExecutor: https://garage.maemo.org/plugins/ggi...51e98721401e72 |
Re: Making Python faster (for fmms initially)
Quote:
|
Re: Making Python faster (for fmms initially)
Quote:
|
Re: Making Python faster (for fmms initially)
Quote:
|
Re: Making Python faster (for fmms initially)
Well, depending on the case, Cython can be a very handy tool...
|
Re: Making Python faster (for fmms initially)
Also :
- Avoid while loop when you can use a "for foo in foo_list" - use xrange instead of range (removed in python 3 so it s not really a good idea for compatibility) ah and of course, Read this : http://wiki.python.org/moin/PythonSpeed/PerformanceTips |
Re: Making Python faster (for fmms initially)
Quote:
it propbably won't make too much difference but if we can get a line count before and one after if there is any kind of difference it can go into the melting pot :) |
Re: Making Python faster (for fmms initially)
Ahaaaaaaaaaaa i'm back! and had time to do it! yay!
First test - normal code 2104 lines of code 580 blank lines 215 code lines Load time from icon click to fully loaded - 10.04 seconds Second Test - Cleared up code 2104 lines of code 0 blank lines 80 code lines Load time from icon click to fully loaded - 9.25 seconds Ok, it was a basic test, but i ran them side by side for a while with nothing else loaded... the load times where almost exactly the same each time. So... yes blank lines do affect load up! My next test, later this week will be to remove redundant lines into sensible functions - should cut my lines of code down a lot...... so will compare that against these results. :D quick edit :- Third - Cleared up code!! 1469 lines of code 0 blank lines 80 code lines Load time from icon click to fully loaded - 8.40 (5 tests , from 8.09 to 8.60) (Sorry code lines probably don't mean much - i meant comment lines!) |
Re: Making Python faster (for fmms initially)
noobmonk3y - so you just saved 20% of the load time by scrubbing your code?
thats remarkable (tho extreme, we like comments) i did not expect the difference to be so great. if we could strip them from debs as part of packaging for most python apps that would be wicked (keep them in source of course) |
Re: Making Python faster (for fmms initially)
@noobmonkey: use the import trick from my presentation, that way you can save the time required for parsing (empty lines or not).
|
Re: Making Python faster (for fmms initially)
Quote:
Code:
compile:I haven't done any performance testing, but if packagers don't do this, and the Python app is being run as user (and it's installed in /opt or /usr), Python will have to do parsing every time: it won't be able to write the bytecode back to disk for next time, as "user" usually won't have permission to the install directory. |
Re: Making Python faster (for fmms initially)
An alternative for handling this ^ is in the postinst/postrm script on the device, that way the debs are kept smaller and apt needs to track less files (at the cost of doing that yourself and a longer install time). Plus, should a system update be shipped with a pyc incompatible Python version, reinstalling the packages updates the pyc/pyo-s without any maintainer intervention.
|
Re: Making Python faster (for fmms initially)
Precompiling should save quite a lot on parsing as it needs to simple read less bytes from storage and doesn't have to do the parse part.
|
Re: Making Python faster (for fmms initially)
Quote:
But remember that the time it took to start the program always gets spread out over how long time you actually use the program. 10s to start for an application that you use 2s (for example getting temperature data from your favourite source and displaying it) is painful. 10s to start for an application that you use for 20m is something quite different. But sure, it's always nice when applications start almost before you knew you wanted to use them. :) |
Re: Making Python faster (for fmms initially)
Quote:
http://www.debian.org/doc/packaging-..._packages.html 2.6 Modules Byte-Compilation |
Re: Making Python faster (for fmms initially)
Quote:
Prioritize this nugget wherever you want. |
Re: Making Python faster (for fmms initially)
Quote:
And I wonder if use of in-line comments versus separate-line comments makes a difference? Greg, can you test that also? |
Re: Making Python faster (for fmms initially)
Quote:
|
Re: Making Python faster (for fmms initially)
Quote:
I find it not a good solution to strip comments and remove empty lines for speed sacrificing readability, therefore increasing the probability of bugs and making debugging harder. |
Re: Making Python faster (for fmms initially)
Quote:
http://wiki.debian.org/NonMaintainer...w&redirect=NMU |
| All times are GMT. The time now is 10:31. |
vBulletin® Version 3.8.8