Skip to content

Instantly share code, notes, and snippets.

@marcoaaguiar
Created April 1, 2020 20:47
Show Gist options
  • Save marcoaaguiar/08907080908fc2feaefda7bfadf9c590 to your computer and use it in GitHub Desktop.
Save marcoaaguiar/08907080908fc2feaefda7bfadf9c590 to your computer and use it in GitHub Desktop.
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