473,789 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1150
甜瓜 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.comwro te:
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.w eb.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(lambd a: 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.w eb.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(lamb da: 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(lambd a: 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(lambd a: 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.w eb.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
1437
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
9290
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. # No warranty express or implied for the accuracy, fitness to purpose
0
1028
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. So I am writing this mail with a lot of hopes. Ex Some flags that are enabled when -O is enabled is -fomit-frame-pointer -fdefer-pop etc.I would like to know all the flags that will be enabled. I am writing this mail with a lot of hopes; I...
5
1568
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) multiplication operations as in: if a == 0 return 0 if a == 1 return b return a*b
4
2116
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 optimization. It # does this by throwing an exception if it is # it's own grandparent, and catching such # exceptions to recall the stack.
3
5176
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
2599
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) ------ int a=10000,b,c=2800,d,e,f,g;main(){for(;b-c;)f=a/5; for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f*a, f=d%--g,d/=g--,--b;d*=b);}
206
8376
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 footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
4
1394
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 comoiled, const char *restrict parttern, int cflags) " or something like that " int strnlen(const char FAR *s, int count)" what does the "restrict" or "FAR" means? --thanks advance
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.