Skip to content

--disable-gil is slower for recursive logic compared to using GIL. #128856

@free-y

Description

@free-y

Bug report

Bug description:

I found that the nogil build performs worse for recursive logic(5s vs 17s in my test) but slightly better for iterative logic.

The Dockerfile for building Python with GIL is exactly the same as the Docker official Python image Dockerfiles for Debian.

Here is the command I use to build the image.

$ docker build -f gil.Dockerfile --no-cache --tag python:3.13.0-gil-slim-bullseye .
$ docker build -f nogil.Dockerfile --no-cache --tag python:3.13.0-nogil-slim-bullseye .

Here are my test results.

freey@freey-dc7:~$ cat factorial_iterative.py
import time
def factorial_iterative(n):
    if n < 0:
        raise ValueError("Input must be a non-negative integer.")
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result

start=time.time()
factorial_iterative(150000)
print(time.time()-start)
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=1 python:3.13.0-gil-slim-bullseye python factorial_iterative.py
4.989101409912109
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=1 python:3.13.0-nogil-slim-bullseye python factorial_iterative.py
3.2846310138702393
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=0 python:3.13.0-nogil-slim-bullseye python factorial_iterative.py
3.2800350189208984
freey@freey-dc7:~$ cat factorial_recursive.py
import time
import sys
sys.setrecursionlimit(200000)
def factorial_recursive(n):
    if n < 0:
        raise ValueError("Input must be a non-negative integer.")
    if n == 0 or n == 1:
        return 1
    return n * factorial_recursive(n - 1)

start=time.time()
factorial_recursive(150000)
print(time.time()-start)
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=1 python:3.13.0-gil-slim-bullseye python factorial_recursive.py
5.0271947383880615
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=1 python:3.13.0-nogil-slim-bullseye python factorial_recursive.py
17.349823474884033
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=0 python:3.13.0-nogil-slim-bullseye python factorial_recursive.py
16.113397121429443
freey@freey-dc7:~$ cat fib_iterative.py
import time
def fib_iterative(N):
    if N < 2:
        return N
    a, b = 0, 1
    for _ in range(2, N + 1):
        a, b = b, a + b
    return b

start = time.time()
fib_iterative(1000000)
print(time.time() - start)
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=1 python:3.13.0-gil-slim-bullseye python fib_iterative.py
6.269308805465698
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=1 python:3.13.0-nogil-slim-bullseye python fib_iterative.py
6.151983976364136
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=0 python:3.13.0-nogil-slim-bullseye python fib_iterative.py
6.1401708126068115
freey@freey-dc7:~$ cat fib_recursive.py
import time
def fib_recursive(N):
    if N<2:
        return N
    else:
        return fib_recursive(N-1)+fib_recursive(N-2)

start=time.time()
fib_recursive(40)
print(time.time()-start)
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=1 python:3.13.0-gil-slim-bullseye python fib_recursive.py
9.034321546554565
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=1 python:3.13.0-nogil-slim-bullseye python fib_recursive.py
16.576115608215332
freey@freey-dc7:~$ docker run -it --rm -v $(pwd):/tmp -w /tmp -e PYTHON_GIL=0 python:3.13.0-nogil-slim-bullseye python fib_recursive.py
16.53062415122986

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions