Basic optimization of python.
Lou Pecora
pecora at anvil.nrl.navy.mil
Wed Apr 9 10:22:01 EDT 2008
In article <877if7b5h9.fsf at mulj.homelinux.net>,
Hrvoje Niksic <hniksic at xemacs.org> wrote:
> "Diez B. Roggisch" <deets at nospam.web.de> writes:
>
> >> 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
More information about the Python-list
mailing list