Reply
Thread Tools
thp's Avatar
Posts: 1,391 | Thanked: 4,272 times | Joined on Sep 2007 @ Vienna, Austria
#41
Originally Posted by Khertan View Post
- use xrange instead of range (removed in python 3 so it s not really a good idea for compatibility)
Actually, in Python 2.x, xrange() returns an iterator, and range() creates a list. In Python 3.x, range() returns an iterator and xrange() does not exist. If you use xrange(), the 2to3 tool can automatically convert it to Python 3.x's range(). If you use range() in 2.x code, the 2to3 tool doesn't know if you really need a list or if an interator is sufficient (e.g. you could go through the list twice, but not through the iterator, etc..).

But you are right - using xrange() is faster, because it can generate items "on the fly" whereas range() creates a list first.

See also: http://diveintopython3.org/porting-c...o3.html#xrange
 

The Following 2 Users Say Thank You to thp For This Useful Post:
noobmonkey's Avatar
Posts: 3,203 | Thanked: 1,391 times | Joined on Nov 2009 @ Worthing, England
#42
Originally Posted by Texrat View Post
I agree, although with good formatting and use of descriptive variable names, code can be highly self-documenting.

And I wonder if use of in-line comments versus separate-line comments makes a difference? Greg, can you test that also?
Yup can do - working through my code at the moment - still working with the commented version (With less empty spaces) at the moment - so once i'm done i'll do 2 versions, one inline and one new lines... will see what happens...

I'm also intrigued as i know nothing about pyo files?! - am i amble to compile my python files to make them run faster?

Sorry if it's obvious - really am still very new to this
__________________
----------- Follow me on Twitter here
----------- My Photography Website and Blog is here
----------- Author of the N900 Health Check Application ----------- New Version in Extras Devel (Dec 2010 - 2.9.10)
----------- Are you on the N900 World Map? - http://pininthemap.com/maemo - masterpin: shotgun
----------- What apps do you want to see on the n900 or in MeeGo in the future? -
 
epage's Avatar
Posts: 1,684 | Thanked: 1,562 times | Joined on Jun 2008 @ Austin, TX
#43
Originally Posted by noobmonkey View Post
Yup can do - working through my code at the moment - still working with the commented version (With less empty spaces) at the moment - so once i'm done i'll do 2 versions, one inline and one new lines... will see what happens...

I'm also intrigued as i know nothing about pyo files?! - am i amble to compile my python files to make them run faster?

Sorry if it's obvious - really am still very new to this
This seems to have a good description
Some tips for experts:
* When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in ‘.pyo’ files. The optimizer currently doesn't help much; it only removes assert statements. When -O is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode.
* Passing two -O flags to the Python interpreter (-OO) will cause the bytecode compiler to perform optimizations that could in some rare cases result in malfunctioning programs. Currently only __doc__ strings are removed from the bytecode, resulting in more compact ‘.pyo’ files. Since some programs may rely on having these available, you should only use this option if you know what you're doing.
* A program doesn't run any faster when it is read from a ‘.pyc’ or ‘.pyo’ file than when it is read from a ‘.py’ file; the only thing that's faster about ‘.pyc’ or ‘.pyo’ files is the speed with which they are loaded.
* When a script is run by giving its name on the command line, the bytecode for the script is never written to a ‘.pyc’ or ‘.pyo’ file. Thus, the startup time of a script may be reduced by moving most of its code to a module and having a small bootstrap script that imports that module. It is also possible to name a ‘.pyc’ or ‘.pyo’ file directly on the command line.
* The module ‘compileall’{} can create ‘.pyc’ files (or ‘.pyo’ files when -O is used) for all modules in a directory.
http://www.network-theory.co.uk/docs...thonfiles.html
__________________
770, n810, n900, Ideapad S10-3t
TheOneRing, DialCentral, Gonvert, Quicknote, Multilist, ejpi, nQa, Waters of Shiloah
Programming Blog
 

The Following 4 Users Say Thank You to epage For This Useful Post:
epage's Avatar
Posts: 1,684 | Thanked: 1,562 times | Joined on Jun 2008 @ Austin, TX
#44
Originally Posted by noobmonkey View Post
Yup can do - working through my code at the moment - still working with the commented version (With less empty spaces) at the moment - so once i'm done i'll do 2 versions, one inline and one new lines... will see what happens...

I'm also intrigued as i know nothing about pyo files?! - am i amble to compile my python files to make them run faster?

Sorry if it's obvious - really am still very new to this
Have you done benchmarks comparing the following?
  • Full .py file
  • Stripped .py file
  • .pyc file
Seems like stripping shouldn't matter if you have pyc's and would save time/hassle.
__________________
770, n810, n900, Ideapad S10-3t
TheOneRing, DialCentral, Gonvert, Quicknote, Multilist, ejpi, nQa, Waters of Shiloah
Programming Blog
 

The Following User Says Thank You to epage For This Useful Post:
noobmonkey's Avatar
Posts: 3,203 | Thanked: 1,391 times | Joined on Nov 2009 @ Worthing, England
#45
nope i haven't didnt even know what a pyc file was until today
Can do though!
__________________
----------- Follow me on Twitter here
----------- My Photography Website and Blog is here
----------- Author of the N900 Health Check Application ----------- New Version in Extras Devel (Dec 2010 - 2.9.10)
----------- Are you on the N900 World Map? - http://pininthemap.com/maemo - masterpin: shotgun
----------- What apps do you want to see on the n900 or in MeeGo in the future? -
 

The Following 2 Users Say Thank You to noobmonkey For This Useful Post:
lcuk's Avatar
Posts: 1,635 | Thanked: 1,816 times | Joined on Apr 2008 @ Manchester, England
#46
epage
of course comments are important in debugging and reading back code - which is why i said we like them
it does serve as a proof of concept over page after page of whitespace and comment overbloat (i am guilty of this!) and in part why I said about removing them as a build option and not from the source totally.
since this is effectively what the .pyc compiled file should do I doubt we will need to go to such extreme measures.

good that people are getting involved though enjoying this thread
__________________
liqbase sketching the future.
like what i say? hit the Thanks, thanks!
twitter.com/lcuk
 

The Following User Says Thank You to lcuk For This Useful Post:
Posts: 74 | Thanked: 142 times | Joined on Oct 2009 @ Chicago, US
#47
Implementing parts of the code as a C/C++ python extension is an option.

A few years ago I implemented a video processing application in Python/C/C++. All the actual video processing was done in C (as an extension, using SWIG), while everything else was in Python with PyQt for the GUI. One problem was that one widget had to do a large number of drawing operations, which was slow using PyQt. The solution was to implement the widget in C++ . I used SIP (the tool which is used to create PyQt) to create create Python bindings for the widget, so it could easily be used with PyQt.

Last edited by pinsh; 2010-04-27 at 00:06.
 

The Following 5 Users Say Thank You to pinsh For This Useful Post:
epage's Avatar
Posts: 1,684 | Thanked: 1,562 times | Joined on Jun 2008 @ Austin, TX
#48
Originally Posted by lcuk View Post
epage
of course comments are important in debugging and reading back code - which is why i said we like them
it does serve as a proof of concept over page after page of whitespace and comment overbloat (i am guilty of this!) and in part why I said about removing them as a build option and not from the source totally.
since this is effectively what the .pyc compiled file should do I doubt we will need to go to such extreme measures.

good that people are getting involved though enjoying this thread
My concern was about the build option and not at debug time and is why I'm advocating pyc's as you mentioned.

I did remember last night something missing in the python.org optimization page, __slot__ . __slot__ saves memory overhead when you have many instances of a small object. I don't know if that would be relevant to any apps people are developing here but its still worth noting.

See http://docs.python.org/reference/datamodel.html#slots
__________________
770, n810, n900, Ideapad S10-3t
TheOneRing, DialCentral, Gonvert, Quicknote, Multilist, ejpi, nQa, Waters of Shiloah
Programming Blog
 

The Following 3 Users Say Thank You to epage For This Useful Post:
epage's Avatar
Posts: 1,684 | Thanked: 1,562 times | Joined on Jun 2008 @ Austin, TX
#49
I'd be curious to hear about profiling results.

What I've done to profile my startup in the past is to comment out the gtk.main() call and run it through a profiler.
Code:
$ python -m cProfile -o .profile PROGRAM
$ python -m pstats .profile
> sort tottime
> show
(From memory, so forgive me if I messed up on some pstats commands).

I was looking through the fmms source code. I have a feeling that some things might be slow (ctypes.CDLL(...)) but I can't say for sure to judge whether these calls should be made lazily (either by thread or upon first use). Dialcentral has historically worked to get started as soon as possible and then pushed off a lot of initialization to a thread, including importing various components. Sadly I can't say how much that helps besides the network communication.

Also something I just remembered is there can be performance differences in how you handle calls to "show"/"show_all" in your code (as judged by a profiler).
__________________
770, n810, n900, Ideapad S10-3t
TheOneRing, DialCentral, Gonvert, Quicknote, Multilist, ejpi, nQa, Waters of Shiloah
Programming Blog
 

The Following 2 Users Say Thank You to epage For This Useful Post:
Khertan's Avatar
Posts: 1,012 | Thanked: 817 times | Joined on Jul 2007 @ France
#50
In case also you are interested ... i ve also write a piece of code to trap every untraped exception to be able to display dialog to user asking him if it want to report it to my bugtracker. (Implemented in pygtkeditor 3.0.15).

And of course this cannot works with b.m.o but i ve made a small php script to be use with flyspray
 

The Following User Says Thank You to Khertan For This Useful Post:
Reply

Tags
performance, python


 
Forum Jump


All times are GMT. The time now is 10:13.