|
|
The just in time compiler in PyPy is a complicated piece of software. It can run some programs very fast, while others it can't yet or it'll never be able to. This is a non-exhaustive list of good and bad practices when writing code targeting PyPy, so it'll run fast.
|
|
|
|
|
|
Attribute access
|
|
|
================
|
|
|
==== General notes ====
|
|
|
|
|
|
Attribute access is fast if arguments are constant. For example:
|
|
|
Simple is better than complex. JIT is not very smart, you have to let it find out what your code does. The simpler your code is the better it'll run. Also is applies to using well-known, but unwritten coding practices. There are many optimizations that optimize common usage pattern against an uncommon usage pattern.
|
|
|
|
|
|
{{{
|
|
|
#!python
|
|
|
==== Attribute access ====
|
|
|
|
|
|
x.a = 3
|
|
|
}}}
|
|
|
Attribute access for new style classes is ver fast if arguments are constant. For example:
|
|
|
|
|
|
{{{x.a = 3}}}
|
|
|
or even
|
|
|
|
|
|
{{{
|
|
|
#!python
|
|
|
|
|
|
setattr(x, 'a', 3)
|
|
|
}}}
|
|
|
|
|
|
{{{setattr(x, 'a', 3)}}}
|
|
|
will be much faster than a dynamic version:
|
|
|
{{{setattr(x, 'a' + some_variable, 3)}}}
|
|
|
|
|
|
{{{
|
|
|
#!python
|
|
|
==== New and old style classes ====
|
|
|
|
|
|
setattr(x, 'a' + some_variable, 3)
|
|
|
}}} |
|
|
New style classes are faster than old style classes. Both are much faster than classes that inherit from both new and oldstyle classes. Avoid the latter at all cost. |
|
|
\ No newline at end of file |