[Python-Dev] Release of astoptimizer 0.3

Stefan Behnel stefan_ml at behnel.de
Tue Sep 11 20:58:19 CEST 2012


Nick Coghlan, 11.09.2012 14:57:
> On Tue, Sep 11, 2012 at 8:41 PM, Victor Stinner wrote:
>> * Loop: replace range() with xrange() on Python 2, and list with
>> tuple. Examples:
>>>> - for x in range(n): ... => for x in xrange(n): ...
>> - for x in [1, 2, 3]: ... => for x in (1, 2, 3): ...
>> Name lookup optimisations again: not in the standard library.

I assume you meant the "range" part, not the second example (which will end
up caching a constant tuple).
>> * Evaluate unary and binary operators, subscript and comparaison if all
>> arguments are constants. Examples:
>>>> - 1 + 2 * 3 => 7
>> - not True => False
>> - "abc" * 3 => "abcabcabc"
>> - abcdef[:3] => abc
>> - (2, 7, 3)[1] => 7
>> - frozenset("ab") | frozenset("bc") => frozenset("abc")

That's a name lookup, too.
>> - None is None => True
>> - "2" in "python2.7" => True
>> - "def f(): return 2 if 4 < 5 else 3" => "def f(): return 2"
>> Yep, literals are good.

Except that evaluating something like '"abc" * constant' can eat up all
memory, imagine this code:
 KILL_MEMORY = sys.argv[1] == 'NEVER PASS THIS VALUE'
 def test():
 if KILL_MEMORY:
 return 'abc' * 10000000000000000000000000000
 else:
 return 'abc'
Stefan


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /