Created
June 18, 2024 13:06
-
-
Save Dimfred/b0745a0485a67c56306a8d540666a4e5 to your computer and use it in GitHub Desktop.
Python Gotta go fast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sysconfig | |
import time | |
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor | |
class Stopwatch: | |
def __init__(self): | |
self.start = time.perf_counter() | |
def __call__(self): | |
stop = time.perf_counter() | |
took = stop - self.start | |
self.start = stop | |
return f"{took:.3f}s" | |
def print_gil(): | |
if sysconfig.get_config_var("Py_GIL_DISABLED"): | |
print("GIL is disabled") | |
else: | |
print("GIL is enabled") | |
sw = Stopwatch() | |
N_LOOPS = 50 | |
N_NUMBERS = 1_000_000 | |
def sum_random(n): | |
return sum(random.random() for i in range(n)) | |
def main(): | |
print_gil() | |
sw() | |
for i in range(N_LOOPS): | |
sum_random(N_NUMBERS) | |
print("SingleThreaded:", sw()) | |
print_gil() | |
sw() | |
with ThreadPoolExecutor(max_workers=8) as pool: | |
for i in range(N_LOOPS): | |
pool.submit(sum_random, N_NUMBERS) | |
print("ThreadPoolExecutor:", sw()) | |
print_gil() | |
sw() | |
with ProcessPoolExecutor(max_workers=8) as pool: | |
for i in range(N_LOOPS): | |
pool.submit(sum_random, N_NUMBERS) | |
print("ProcessPoolExecutor:", sw()) | |
print_gil() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment