Skip to content

Instantly share code, notes, and snippets.

@xoxwgys56
Created October 19, 2022 02:39
Show Gist options
  • Save xoxwgys56/d14e9f6f7800ba23c6d0af38b02fb1dc to your computer and use it in GitHub Desktop.
Save xoxwgys56/d14e9f6f7800ba23c6d0af38b02fb1dc to your computer and use it in GitHub Desktop.
no-gil test

No GIL

REF: https://github.com/colesbury/nogil

run test env

# run and remove container
docker run --rm $(docker build -q .)

test result

enable gil took about more than 3 times performance time.

no-gil

nogil=True
fibo: 9227465 elapsed time 16.095300436019897.
fibo: 9227465 elapsed time 16.52220582962036.
fibo: 9227465 elapsed time 16.68897533416748.
fibo: 9227465 elapsed time 16.693021774291992.
fibo: 9227465 elapsed time 16.901812314987183.
fibo: 9227465 elapsed time 16.987688779830933.
fibo: 9227465 elapsed time 20.314191818237305.
fibo: 9227465 elapsed time 20.323174953460693.

gil

nogil=False
fibo: 9227465 elapsed time 66.92367959022522.
fibo: 9227465 elapsed time 70.01325607299805.
fibo: 9227465 elapsed time 70.25191235542297.
fibo: 9227465 elapsed time 71.103266954422.
fibo: 9227465 elapsed time 71.86525321006775.
fibo: 9227465 elapsed time 71.93268299102783.
fibo: 9227465 elapsed time 72.1888496875763.
fibo: 9227465 elapsed time 72.25596213340759.
FROM nogil/python
WORKDIR /
# If enable gil uncomment below:
# ENV PYTHONGIL=1
COPY ./main.py /main.py
CMD ["python", "/main.py"]
import sys
from concurrent.futures import ThreadPoolExecutor
print(f"nogil={getattr(sys.flags, 'nogil', False)}")
def fib(num):
"""fibonacci"""
if num < 2:
return 1
return fib(num-1) + fib(num-2)
if __name__ == "__main__":
THREADS = 8
if len(sys.argv) > 1:
THREADS = int(sys.argv[1])
with ThreadPoolExecutor(max_workers=THREADS) as executor:
for _ in range(THREADS):
executor.submit(lambda: print(f"fibo: {fib(34)}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment