Created
March 31, 2021 03:34
-
-
Save sandeepkumar-skb/9405eb9f42a3945791d920b488ba993f to your computer and use it in GitHub Desktop.
This python threading example demonstrates python threading in compute bound situation.
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 | |
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