Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
free-y opened this issue Jan 15, 2025 · 1 comment
Open

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

free-y opened this issue Jan 15, 2025 · 1 comment
Labels
performance Performance or resource usage topic-free-threading type-bug An unexpected behavior, bug, or error

Comments

@free-y
Copy link

free-y commented Jan 15, 2025

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

@free-y free-y added the type-bug An unexpected behavior, bug, or error label Jan 15, 2025
@Eclips4 Eclips4 added topic-free-threading performance Performance or resource usage labels Jan 15, 2025
@Eclips4
Copy link
Member

Eclips4 commented Jan 15, 2025

Hello.
Free-threading build is still WIP. You could run this benchmark on current main and probably get better results since there have been a lot of improvements since 3.13 (for example, thread-local bytecode to enable an adaptive interpreter in free-threaded build). However, performance regression is expected: https://docs.python.org/uk/dev/howto/free-threading-python.html#single-threaded-performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage topic-free-threading type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants