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

**URL:** <https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976>\
**Category:** Python Help\
**Tags:** performance\
**Created:** [May 22, 2024, 3:12pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976 "2024-05-22T15:12:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabien-michel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fabien-michel/32/20022_2.png) [@fabien-michel](https://discuss.python.org/u/fabien-michel)\
**Post date:** [May 22, 2024, 3:13pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/1 "2024-05-22T15:13:00Z")

</div>

Hi,  
Debian’s python installed inside a docker container is faster than the official python from docker. How is it possible ?

```python
# Dockerfile-debian
FROM debian:sid
RUN apt update && apt install -y python3.12

```

```python
# Python code for benchmark : test.py
import sys
import timeit
def test_function():
    numbers = list(range(1000))
    [num ** 2 for num in numbers]
benchmark_result = timeit.timeit(test_function, number=int(sys.argv[1]) )
print(benchmark_result)

```

Results

```python
$ docker build -f ./Dockerfile-debian -t python_debian .
$ docker run -v .:/app python_debian python3.12 /app/test.py
# 0.7484053150001273

$ docker run -v .:/app python:3.12 python3.12 test.py
# 1.1680918899983226

```

How does debian manage to have better performances than the official python docker image ?

I’ve tried to build python using the same build args than debian, but I’ve ended with an even slower binary.

> **Debian build args**
>
> ```python
> '--enable-shared'
> '--prefix=/usr'
> '--libdir=/usr/lib/x86_64-linux-gnu'
> '--enable-ipv6'
> '--enable-loadable-sqlite-extensions'
> '--with-dbmliborder=bdb:gdbm'
> '--with-computed-gotos'
> '--without-ensurepip'
> '--with-system-expat'
> '--with-dtrace'
> '--with-ssl-default-suites=openssl'
> '--with-wheel-pkg-dir=/usr/share/python-wheels/'
> 'MKDIR_P=/bin/mkdir -p'
> 'CC=x86_64-linux-gnu-gcc'
> 
> ```

---

<div class="post-metadata">

**Author:** ![tiran](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tiran/32/48_2.png) [@tiran](https://discuss.python.org/u/tiran)\
**Post date:** [May 22, 2024, 3:55pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/2 "2024-05-22T15:55:34Z")

</div>

Are you referring to [https://hub.docker.com/\_/python](https://hub.docker.com/_/python)? These are “Docker Official Images”, not official Python images. The images are provided by Docker Community, not by the PSF or Python community.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [May 22, 2024, 4:07pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/3 "2024-05-22T16:07:10Z")

</div>

A while ago debian started to build python without using a libpython.  
They claimed that that change made starting python faster.  
May be that is the difference?

Otherwise it could be differences in compiler optimisation options used.

---

<div class="post-metadata">

**Author:** ![fabien-michel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fabien-michel/32/20022_2.png) [@fabien-michel](https://discuss.python.org/u/fabien-michel)\
**Post date:** [May 22, 2024, 4:08pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/4 "2024-05-22T16:08:19Z")

</div>

Yes, I’m referring to the Python image from the Docker Hub. Sorry, I thought it was made by PSF or the Python community.

---

<div class="post-metadata">

**Author:** ![fabien-michel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fabien-michel/32/20022_2.png) [@fabien-michel](https://discuss.python.org/u/fabien-michel)\
**Post date:** [May 22, 2024, 4:16pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/5 "2024-05-22T16:16:14Z")

</div>

The “benchmark” I’ve used does not measure python boot time, only code execution. The difference is not here.  
I’ve checked python build args using `sysconfig.get_config_vars('CONFIG_ARGS')`  
Debian ones :

```python
'--enable-shared'
'--prefix=/usr'
'--libdir=/usr/lib/x86_64-linux-gnu'
'--enable-ipv6'
'--enable-loadable-sqlite-extensions'
'--with-dbmliborder=bdb:gdbm'
'--with-computed-gotos'
'--without-ensurepip'
'--with-system-expat'
'--with-dtrace'
'--with-ssl-default-suites=openssl'
'--with-wheel-pkg-dir=/usr/share/python-wheels/'
'MKDIR_P=/bin/mkdir -p'
'CC=x86_64-linux-gnu-gcc'

```

Official docker’s python:

```python
'--build=x86_64-linux-gnu'
'--enable-loadable-sqlite-extensions'
'--enable-optimizations'
'--enable-option-checking=fatal'
'--enable-shared'
'--with-lto'
'--with-system-expat'
'--without-ensurepip'
'build_alias=x86_64-linux-gnu'

```

Note that debian does not use `--enable-optimizations`

---

<div class="post-metadata">

**Author:** ![fabien-michel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fabien-michel/32/20022_2.png) [@fabien-michel](https://discuss.python.org/u/fabien-michel)\
**Post date:** [May 22, 2024, 4:20pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/6 "2024-05-22T16:20:38Z")

</div>

I’ve published the full benchmark I’ve made here : [https://github.com/fabien-michel/compare-python](https://github.com/fabien-michel/compare-python)

It compare python from different Linux distributions using docker and local Python.  
Here are the results on my machine which run Arch Linux: (percent are diff with official Docker’s Python)

```python
debian-on-docker 0.7246799930017005	-34.072282393951454%
ubuntu-on-docker 0.7611594549998699	-30.75356559169216%
arch-on-docker 0.9717710979984986	-11.593184377570633%
local 1.035610535998785	-5.785395448310301%
fedora-on-docker 1.035977895000542	-5.751974985814081%
official-docker 1.099203824000142	0.0%

```

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [May 22, 2024, 8:26pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/7 "2024-05-22T20:26:30Z")

</div>

> [@fabien-michel](#):
>
> Note that debian does not use `--enable-optimizations`

What matters are the C compiler used, gcc I would expect, and the C compiler options.

Even if --enable-optimisations is not passed to python configure script that does mean there are no optimisations. Its typical to pass them in env vars like CFLAGS and LDFLAGS.

Do you know what they where when python was compiled?

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [May 22, 2024, 9:07pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/8 "2024-05-22T21:07:27Z")

</div>

> Do you know what they where when python was compiled?

This is the makefile used by Debian to build the Python 3.11 package  
in their bookworm (current stable) release:

> **[debian/rules · bookworm · Python Interpreter / python3 · GitLab](https://salsa.debian.org/cpython-team/python3/-/blob/bookworm/debian/rules)**
>
> Python3.x Packaging

I assume that’s what the OP installed in their container anyway.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [May 22, 2024, 9:13pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/9 "2024-05-22T21:13:43Z")

</div>

> [@fungi](#):
>
> This is the makefile used by Debian to build the Python 3.11 package  
> in their bookworm (current stable) release:

And this is the key line

```python
DPKG_CFLAGS := $(shell $(dpkg_buildflags) --get CFLAGS)

```

you will need to know what that evaluates as.

---

<div class="post-metadata">

**Author:** ![fabien-michel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fabien-michel/32/20022_2.png) [@fabien-michel](https://discuss.python.org/u/fabien-michel)\
**Post date:** [May 22, 2024, 9:28pm UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/10 "2024-05-22T21:28:29Z")

</div>

Ok thanks for your search. Python seems to store all config variables. It cam be retrieved using `sysconfig.get_config_vars()`.

Here CFLAGS et LDFLAGS from debian’s python:

```python
    "CFLAGS": "-fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall",
    "LDFLAGS": " -Wl,-z,relro -g -fwrapv -O2 ",

```

And CFLAGS and LDFLAGS from docker’s python:

```python
    "CFLAGS": "-fno-strict-overflow -Wsign-compare -DNDEBUG -g -O3 -Wall",
    "LDFLAGS": "",

```

I’m really surprised there is so much difference in build options across distributions.

---

<div class="post-metadata">

**Author:** ![tiran](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tiran/32/48_2.png) [@tiran](https://discuss.python.org/u/tiran)\
**Post date:** [May 23, 2024, 4:59am UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/11 "2024-05-23T04:59:49Z")

</div>

> [@barry-scott](#):
>
> Even if --enable-optimisations is not passed to python configure script that does mean there are no optimisations. Its typical to pass them in env vars like CFLAGS and LDFLAGS.

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.

> [@fabien-michel](#):
>
> I’m really surprised there is so much difference in build options across distributions.

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%](https://developers.redhat.com/blog/2020/06/25/red-hat-enterprise-linux-8-2-brings-faster-python-3-8-run-speeds#) with Python 3.8. AFAIK speedups are less impressive in 3.12.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [May 23, 2024, 7:20am UTC](https://discuss.python.org/t/why-python-in-debian-docker-image-is-faster-than-official-dockers-python-image/53976/12 "2024-05-23T07:20:17Z")

</div>

> [@tiran](#):
>
> `--enable-optimization` is used for PGO builds, which makes Python about 10% faster

Thanks for the correction, I’d forgotten about the PGO stuff.
