[Python-Dev] Don't set local variable in a list comprehension or generator (original) (raw)
Victor Stinner victor.stinner at haypocalc.com
Wed May 18 14:21:55 CEST 2011
- Previous message: [Python-Dev] Inconsistent case in directory names for installed Python on Windows
- Next message: [Python-Dev] Don't set local variable in a list comprehension or generator
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi,
''.join(c for c in 'abc') and ''.join([c for c in 'abc']) do create a temporary c variable. In this case, the variable is useless and requires two opcodes: STORE_FAST(c), LOAD_FAST(c). The variable is not available outside the list comprehension/generator.
I would like to remove the variable in these cases to speed up (micro-optimize!) Python.
Remove the variable breaks code using introspection like:
list([locals()['x'] for x in range(3)])
We may detect the usage of introspection (I don't know how hard it is), only optimize trivial cases (like "x for x in ..."), or only optmize with Python is running in optimize mode (python -O or python -OO).
What do you think? Is it useless and/or stupid?
I heard about optimization in the AST tree instead of working on the bytecode. What is the status of this project?
Victor
- Previous message: [Python-Dev] Inconsistent case in directory names for installed Python on Windows
- Next message: [Python-Dev] Don't set local variable in a list comprehension or generator
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]