Why python in Debian Docker image is faster than official Docker's python image?

No, your answer wrong. --enable-optimization is used for PGO builds, which makes Python about 10% faster. PGO cannot be enabled by passing different CFLAGS and LDFLAGS alone. In PGO mode, make first compiles an instrumented build of Python, then runs some tests to create optimization profiles, and finally compiles Python a second time. You trade a slower build for a faster Python.

You can get the actual flags for Python interpreter core from sysconfig.get_config_vars("PY_CORE_CFLAGS") and sysconfig.get_config_vars("PY_CORE_LDFLAGS"). Fedora and RHEL use additional flags like -fno-semantic-interposition, which can increase performance by up to 30% with Python 3.8. AFAIK speedups are less impressive in 3.12.

1 Like