Skip to content

Instantly share code, notes, and snippets.

@agronick
Created Apr 1, 2018
Embed
What would you like to do?
Still in 2018 threads in Python do not make CPU bound tasks faster
import time
from concurrent.futures import ThreadPoolExecutor
import random
start = 0
def print_time():
global start
end = time.time()
print(end - start)
def fib(n):
if 2 > n:
return n
return fib(n-2) + fib(n-1)
def run():
items = list(range(1, 25))
with ThreadPoolExecutor(max_workers=len(items)) as p:
def threaded():
global start
start = time.time()
print('thread', list(p.map(fib, items)))
print_time()
def single_thread():
global start
start = time.time()
print('single', list(map(fib, items)))
print_time()
functs = threaded, single_thread
for i in range(50):
for f in random.sample(functs, len(functs)):
f()
run()
"""
Controlling for the order of the functions and the process being warmed
up the same code runs in about the same time regardless if it is threaded or not.
Last ~7 iterations example output
single [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.0468602180480957
thread [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.04687857627868652
thread [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.04687833786010742
single [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.046877384185791016
thread [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.046872854232788086
single [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.06251001358032227
thread [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.04687786102294922
single [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.06250452995300293
single [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.04687786102294922
thread [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.0624997615814209
thread [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.04687786102294922
single [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
0.06250953674316406
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment