Skip to content

Instantly share code, notes, and snippets.

@sandeepkumar-skb
Created March 31, 2021 03:34
Show Gist options
  • Save sandeepkumar-skb/9405eb9f42a3945791d920b488ba993f to your computer and use it in GitHub Desktop.
Save sandeepkumar-skb/9405eb9f42a3945791d920b488ba993f to your computer and use it in GitHub Desktop.
This python threading example demonstrates python threading in compute bound situation.
import threading
import time as time
def square():
for i in range(1000000):
x = pow(i, 2)
if __name__ == "__main__":
num_iter = 10
start = time.time()
for i in range(num_iter):
square()
stop = time.time()
print("non threading time: {:.3f}".format(stop-start))
start = time.time()
threads = []
for i in range(num_iter):
t = threading.Thread(target=square, args=())
t.start()
threads.append(t)
for t in threads:
t.join()
stop = time.time()
print("threading time: {:.3f}".format(stop-start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment