473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

python optimization

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
4 1595
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

467
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
44
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical...
36
by: Armin Rigo | last post by:
Hi! This is a rant against the optimization trend of the Python interpreter. Sorting a list of 100000 integers in random order takes: * 0.75 seconds in Python 2.1 * 0.51 seconds in Python...
0
by: Praveen, Tayal (IE10) | last post by:
Hi Guys, I am having problems in the following C API program where myOtim_system is callable from python function. The module is listed below - static PyObject * myOptim_system(PyObject...
4
by: tkpmep | last post by:
I use Python to generate a huge amount of data in a .csv file which I then process using Excel. In particular, I use Excel's solver to solve a number of non-linear equation, and then regress the...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
40
by: brad | last post by:
Will len(a_string) become a_string.len()? I was just reading http://docs.python.org/dev/3.0/whatsnew/3.0.html One of the criticisms of Python compared to other OO languages is that it isn't OO...
18
by: Jens | last post by:
I'm starting a project in data mining, and I'm considering Python and Java as possible platforms. I'm conserned by performance. Most benchmarks report that Java is about 10-15 times faster than...
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.