CKP

Pure Python implementations of competitive programming algorithms.

View the Project on GitHub 123jimin/ckp

Optimizing CPython Code

Optimizing pure CPython code is far from intuitive.

In most other languages, you may rely on compilers for various small yet extremely useful optimizations, such as constant propagation, variable lookup, arithmetic operations etc…. However, CPython’s bytecode compiler is doing a bare minimum job, without doing any significant optimizations.

General Rules

These are the most important tips.

Integer Arithmetic

Whether the absolute value of an integer is less than $2^{30}$ or not heavily affects the performance, as that’s the size of a digit CPython uses. Hence, it is extremely important to distinguish between “small” integers (absolute value less than $2^{30}$) and “large” integers.

Boolean Conversion

Bitwise Operations

DivMod

Iteration

Containers