Created
April 1, 2020 20:47
-
-
Save marcoaaguiar/08907080908fc2feaefda7bfadf9c590 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 | |
# Timing function | |
def timeit(func): | |
start = perf_counter() | |
func() | |
print(f"{func.__name__} took {perf_counter()-start}") | |
def task(): | |
sleep(5) | |
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) | |
threads.append(t) | |
t.start() | |
# Wait all to finish | |
for t in threads: | |
t.join() | |
timeit(task) | |
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