-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreads.py
More file actions
33 lines (25 loc) · 748 Bytes
/
threads.py
File metadata and controls
33 lines (25 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import time
from threading import Thread
def ask_user():
start = time.time()
user_input = input('Enter your name: ')
greet = f'Hello, {user_input}'
print(greet)
print(f'ask_user, {time.time() - start}')
def complex_calculation():
start = time.time()
print('Started calculating......')
[x**2 for x in range(20000000)]
print(f'complex_calculation, {time.time() - start}')
start = time.time()
ask_user()
complex_calculation()
print(f'Single thread total time: {time.time() - start}')
thread1 = Thread(target=complex_calculation)
thread2 = Thread(target=ask_user)
start = time.time()
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(f'Two thread total time: {time.time() - start}')