473,387 Members | 1,799 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.

Basic optimization of python.

Howdy,
I wonder whether python compiler does basic optimizations to .py.
Eg:
t = self.a.b
t.c = ...
t.d = ...
..vs.
self.a.b.c = ...
self.a.b.d = ...
which one is more effective? Since each dot invokes a hash table lookup, it
may be time consuming. If the compiler can do expression folding, then no
manual folding is needed.

Again, how about contant calculation?
Eg:
a = 1 + 2
..vs.
a = 3
which one is more effective? Does the compiler calculate the result at
compile time? How about constant spreading?

Best regards,
---
ShenLei

Apr 9 '08 #1
7 1132
甜瓜 wrote:
Howdy,
I wonder whether python compiler does basic optimizations to .py.
Eg:
t = self.a.b
t.c = ...
t.d = ...
.vs.
self.a.b.c = ...
self.a.b.d = ...
which one is more effective? Since each dot invokes a hash table lookup,
it may be time consuming. If the compiler can do expression folding, then
no manual folding is needed.

It can't do that because of no guarantee can be made that self.a has no
side-effects, and consequently doesn't do it.
Again, how about contant calculation?
Eg:
a = 1 + 2
.vs.
a = 3
which one is more effective? Does the compiler calculate the result at
compile time? How about constant spreading?
Algebraic optimizations aren't done AFAIK - and it's debatable if the
mu-secs saved by that would be worth the effort, as if *they* matter, you
obviously are in number-crunching mode and should resort to libs such as
Numpy to do your work.

Diez
Apr 9 '08 #2
On Apr 7, 7:30 am, "Ìð¹Ï" <littlesweetme...@gmail.comwrote:
Howdy,
I wonder whether python compiler does basic optimizations to .py.
Eg:
t = self.a.b
t.c = ...
t.d = ...
.vs.
self.a.b.c = ...
self.a.b.d = ...
which one is more effective? Since each dot invokes a hash table lookup, it
may be time consuming. If the compiler can do expression folding, then no
manual folding is needed.
The compiler can not simply do such folding, as just the act of
looking up an attribute can have all kinds of side effects via the
methods __getattr__ and __getattribute__ . So the second time you look
up self.a the value can differ from the first time, or the attribute
may not exist anymore!
Again, how about contant calculation?
Eg:
a = 1 + 2
.vs.
a = 3
which one is more effective? Does the compiler calculate the result at
compile time? How about constant spreading?
This is safe, and is done:
>>import dis
def f(): x = 1 + 2
...
>>dis.dis(f)
1 0 LOAD_CONST 3 (3)
3 STORE_FAST 0 (x)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE
>>>
- Willem
Apr 9 '08 #3
"Diez B. Roggisch" <de***@nospam.web.dewrites:
>Eg:
a = 1 + 2
.vs.
a = 3
which one is more effective? Does the compiler calculate the result at
compile time? How about constant spreading?

Algebraic optimizations aren't done AFAIK
Just try it:

Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>dis.dis(lambda: 10+5)
1 0 LOAD_CONST 2 (15)
3 RETURN_VALUE
Apr 9 '08 #4
This is safe, and is done:
>
>>>import dis
def f(): x = 1 + 2
...
>>>dis.dis(f)
1 0 LOAD_CONST 3 (3)
3 STORE_FAST 0 (x)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE
>>>>
So I stand corrected. Any idea when that was introduced?

Diez
Apr 9 '08 #5
Hrvoje Niksic wrote:
"Diez B. Roggisch" <de***@nospam.web.dewrites:
>>Eg:
a = 1 + 2
.vs.
a = 3
which one is more effective? Does the compiler calculate the result at
compile time? How about constant spreading?

Algebraic optimizations aren't done AFAIK

Just try it:

Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>dis.dis(lambda: 10+5)
1 0 LOAD_CONST 2 (15)
3 RETURN_VALUE
I remember times when that hasn't been the case - thus my answer.

[GCC 3.4.6 (Ubuntu 3.4.6-6ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Welcome to rlcompleter2 0.96
for nice experiences hit <tabmultiple times
>>import dis
dis.dis(lambda: 1+2)
1 0 LOAD_CONST 1 (1)
3 LOAD_CONST 2 (2)
6 BINARY_ADD
7 RETURN_VALUE
Python 2.4.4 (#2, Mar 7 2008, 04:45:43)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import dis
dis.dis(lambda: 1+2)
1 0 LOAD_CONST 1 (1)
3 LOAD_CONST 2 (2)
6 BINARY_ADD
7 RETURN_VALUE
>>>

But great to know it is done now.

Diez
Apr 9 '08 #6
ShenLei:
t = self.a.b
t.c = ...
t.d = ...
.vs.
self.a.b.c = ...
self.a.b.d = ...
which one is more effective? Since each dot invokes a hash table lookup, it
may be time consuming. If the compiler can do expression folding, then no
manual folding is needed.
Removing dots creating a temporary variable speeds up the program, but
doing it is useful only in special critical spots, like inside certain
large loops, etc.
A similar optimization is to use a local instead of a global:

from foo import bar
def baz(bar=bar):
for i in xrange(100000):
bar(...)

Note that with psyco you often don't need such things.

Bye,
bearophile
Apr 9 '08 #7
In article <87************@mulj.homelinux.net>,
Hrvoje Niksic <hn*****@xemacs.orgwrote:
"Diez B. Roggisch" <de***@nospam.web.dewrites:
Eg:
a = 1 + 2
.vs.
a = 3
which one is more effective? Does the compiler calculate the result at
compile time? How about constant spreading?
Algebraic optimizations aren't done AFAIK

Just try it:

Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>dis.dis(lambda: 10+5)
1 0 LOAD_CONST 2 (15)
3 RETURN_VALUE
Not always. On a Mac running Python 2.4, here's what I get:

In [3]: dis.dis(lambda: 3+4)
1 0 LOAD_CONST 1 (3)
3 LOAD_CONST 2 (4)
6 BINARY_ADD
7 RETURN_VALUE

--
-- Lou Pecora
Apr 9 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: | last post by:
I would like to optimize my classes at run time. The question is where to put my "if" statement. -Inside __main__? if option: class Foo: def __init__: #do stuff else: class Foo: def __init__:
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
0
by: Naveen H.S. | last post by:
Hi I had mailed yesterday about the following topic to know the flags or options that are enabled when -O,-O2,-O3,-Os options are enabled. As mentioned below I was given this mail id to contact....
5
by: Paul McGuire | last post by:
Does Python's run-time do any optimization of multiplication operations, like it does for boolean short-cutting? That is, for a product a*b, is there any shortcutting of (potentially expensive)...
4
by: Crutcher | last post by:
This is fun :) {Note: I take no responsibilty for anyone who uses this in production code} #!/usr/bin/env python2.4 # This program shows off a python decorator # which implements tail call...
3
by: amitsoni.1984 | last post by:
Hi, I need to do a quadratic optimization problem in python where the constraints are quadratic and objective function is linear. What are the possible choices to do this. Thanks Amit
21
by: mm | last post by:
(Yes, I konw whats an object is...) BTW. I did a translation of a pi callculation programm in C to Python. (Do it by your own... ;-) -------- Calc PI for 800 digs(?). (german: Stellen) ------...
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...
4
by: sagi | last post by:
Hello erveryone,I am a newcomer here and the word of c. Here I have a question confused me a lot that when I read codes I found some declaration like that: "int regcomp(regex_t *restrict...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.