python optimization

N

Neal Becker

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?
 
D

David Wilson

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.
 
R

Reinhold Birkenfeld

David said:
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
 
T

Thomas Heller

Reinhold Birkenfeld said:
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.
.... for i in range(to):
.... def adder(amount):
.... return i + amount
.... yield adder
........ print f(42)
....
42
43
44
Seems to work. But observe this:
.... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,317
Members
48,003
Latest member
coldDuece

Latest Threads

Top