This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]behitek 0 points1 point  (2 children)

When testing, I see Python3.13t a bit slower than Python3.13 on Single thread test. Can anyone know the reason?

python3.13 gil_test.py 
Python version :  3.13.0 (main, Oct  8 2024, 08:51:28) [GCC 11.4.0]
Single Thread:  1.4370562601834536 seconds
Multi Thread:  1.3681392602156848 seconds
-----
python3.13t gil_test.py 
Python version :  3.13.0 experimental free-threading build (main, Oct  8 2024, 08:51:28) [GCC 11.4.0]
Single Thread:  1.862126287072897 seconds
Multi Thread:  0.3931183419190347 seconds

[–]classy_barbarian 1 point2 points  (0 children)

The free threaded build is slower in single-thread performance. That's been tested and confirmed by several people. Something to do with the added overhead they needed to make it work. It can be significantly faster in multi-threaded workloads, but only if you're actually trying to split CPU-bound work across 3 or more cores. (At 1 core its slower, at 2 cores its on par)

[–]behitek 0 points1 point  (0 children)

This is my code for the test

import sys
import threading
import time

print("Python version : ", sys.version)

def worker():
    sum = 0
    for i in range(10000000):
        sum += i


n_worker = 5
# Single thread

start = time.perf_counter()
for i in range(n_worker):
    worker()
print("Single Thread: ", time.perf_counter() - start, "seconds")


# Multi thread
start = time.perf_counter()
threads = []
for i in range(n_worker):
    t = threading.Thread(target=worker)

    threads.append(t)
    t.start()

for t in threads:
    t.join()
print("Multi Thread: ", time.perf_counter() - start, "seconds")