Interpreters
- CPython: the standard python implementation written in C
- Jython: written in Java
- IronPython: written in C#
- PyPy: written in RPython (which is itself a statically-typed subset of Python written C that is designed to write interpreters)
Interning
Reusing objects on-demand
Integer
At startup, CPython pre-loads(catches) a global list of integers in the range[-5, 256], any time an integer is referenced in the range, python will use the cached version of that object.
Optimization strategy: small integers show up often
string
Not all strings are automatically interned
As the python code is compiled, identifiers are interned
peephole
an optimization occur at compile time
pre-calculate constant expressions
- numeric calculation: 24 * 60 (store 1440)
- short seqences length < 20: ‘abc’ * 3 (store ‘abcabcabc’)
membership tests (if e in [1, 2, 3]: …)
mutable replaced by immutable
- list -> tuples
- set -> frozenset
- set membership is much faster than list and tuple