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
Dramatic slowdown with random.random on free-threading build. #118392
Comments
|
cc @colesbury |
|
It'd be great to have the default random number generator be safe and efficient when called from multiple threads, but I think that's a bigger change that requires more discussion and maybe a PEP. In the meantime, I think the advice should be to use individual def monte_carlo_pi_part(n: int, idx: int, results: list[int]) -> None:
r = random.Random()
count = 0
for i in range(n):
x = r.random()
y = r.random()
if x*x + y*y <= 1:
count += 1
results[idx] = countI think that you also would need #118112 (or deferred reference counting) for the program to be efficient, or otherwise the reference count contention is going to be a bottleneck. |
Yeah, it works. so it should be included somewhere in the migration guide. |
|
We could also consider adopting a strategy like Go does, at least for the free-threaded build:
|


This slowdown was found with one of my favorite benchmarks, which is calculating the pi value with the Monte Carlo method.
Acquiring critical sections for random methods causes this slowdown.
Removing
@critical_sectionfrom the method, which usesgenrand_uint32and then updatinggenrand_uint32to use atomic operation makes the performance acceptable.Linked PRs
The text was updated successfully, but these errors were encountered: