Created
April 1, 2020 21:01
-
-
Save marcoaaguiar/da1b341928a9cada24d8571805280da5 to your computer and use it in GitHub Desktop.
This file contains 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 threading | |
from time import perf_counter, sleep, time | |
from first_example import timeit | |
def task(n=35): | |
if n == 1: | |
return 0 | |
if n == 2: | |
return 1 | |
return task(n - 1) + task(n - 2) | |
def run_tasks_synchronous(times=5): | |
for _ in range(times): | |
task() | |
def run_tasks_threaded(times=5): | |
threads = [] | |
# Create and start threads | |
for _ in range(times): | |
t = threading.Thread(target=task, daemon=True) | |
threads.append(t) | |
t.start() | |
# Wait all to finish | |
for t in threads: | |
t.join() | |
timeit(fibonacci) | |
timeit(run_tasks_synchronous) | |
timeit(run_tasks_threaded) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment