By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

python optimization

P: n/a
I use cpython. I'm accustomed (from c++/gcc) to a style of coding that is
highly readable, making the assumption that the compiler will do good
things to optimize the code despite the style in which it's written. For
example, I assume constants are removed from loops. In general, an entity
is defined as close to the point of usage as possible.

I don't know to what extent these kind of optimizations are available to
cpython. For example, are constant calculations removed from loops? How
about functions? Is there a significant cost to putting a function def
inside a loop rather than outside?

Sep 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
For the most part, CPython performs few optimisations by itself. You
may be interested in psyco, which performs several heavy optimisations
on running Python code.

http://psyco.sf.net/

Defining a function inside a loop in CPython will cause a new function
object to be created each and every time the loop runs. No such
automatic optimisation is performed there. For the most part, this lack
of opimisation not only simplifies the CPython implementation, but also
causes code to act much more closely to how it was defined, which is
good for new and advanced users alike.

Other than psyco, IronPython and PyPy are two projects which you might
be interested in if execution performance is of interest to you.

http://www.ironpython.com/
http://codespeak.net/pypy/
David.

Sep 15 '05 #2

P: n/a
David Wilson wrote:
For the most part, CPython performs few optimisations by itself. You
may be interested in psyco, which performs several heavy optimisations
on running Python code.

http://psyco.sf.net/

Defining a function inside a loop in CPython will cause a new function
object to be created each and every time the loop runs. No such
automatic optimisation is performed there. For the most part, this lack
of opimisation not only simplifies the CPython implementation, but also
causes code to act much more closely to how it was defined, which is
good for new and advanced users alike.


More importantly, since Python supports lexical scopes, a function defined
in a loop could be different each time it is defined, e.g.

def getadders(to):
for i in range(to):
def adder(amount):
return i + amount
yield adder
Reinhold
Sep 15 '05 #3

P: n/a
Reinhold Birkenfeld <re************************@wolke7.net> writes:
David Wilson wrote:
For the most part, CPython performs few optimisations by itself. You
may be interested in psyco, which performs several heavy optimisations
on running Python code.

http://psyco.sf.net/

Defining a function inside a loop in CPython will cause a new function
object to be created each and every time the loop runs. No such
automatic optimisation is performed there. For the most part, this lack
of opimisation not only simplifies the CPython implementation, but also
causes code to act much more closely to how it was defined, which is
good for new and advanced users alike.


More importantly, since Python supports lexical scopes, a function defined
in a loop could be different each time it is defined, e.g.

def getadders(to):
for i in range(to):
def adder(amount):
return i + amount
yield adder


Hehe. Dangerous code.
def getadders(to): .... for i in range(to):
.... def adder(amount):
.... return i + amount
.... yield adder
....
for f in getadders(3): .... print f(42)
....
42
43
44
Seems to work. But observe this:
def getadders(to): .... for i in range(to):
.... def adder(amount):
.... return i + amount
.... yield adder
....
funcs = [x for x in getadders(3)]
for f in funcs: .... print f(42)
....
44
44
44


Thomas
Sep 15 '05 #4

P: n/a
Reinhold Birkenfeld wrote:
David Wilson wrote:
For the most part, CPython performs few optimisations by itself. You
may be interested in psyco, which performs several heavy optimisations
on running Python code.

http://psyco.sf.net/


I might be, if it supported x86_64, but AFAICT, it doesn't.

Sep 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.