-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithread.py
63 lines (45 loc) · 1.65 KB
/
multithread.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import threading
import time
# Get the start time
start_time = time.time()
# Function to calculate the sum of a portion of the range
def calculate_partial_sum(start, end, result):
partial_sum = 0
for i in range(start, end):
partial_sum += i
result.append(partial_sum)
# Define the number of threads
num_threads = 10
range_start = 1
range_end = 1000000001
step = (range_end - range_start) // num_threads
# Create a list to store thread objects
threads = []
# Create a list to store results from each thread
results = []
# Loop through a range of threads to perform parallel calculations
for i in range(num_threads):
# Calculate the start and end values for the current thread's task
start = range_start + i * step
end = start + step if i < num_threads - 1 else range_end
# Initialize an empty list to store the result of the current thread's calculation
result = []
# Create a new thread with the calculate_partial_sum function as the target,
# and pass the start, end, and result as arguments to the function
thread = threading.Thread(
target=calculate_partial_sum, args=(start, end, result))
# Append the thread to a list of threads
threads.append(thread)
# Append the result list to a list of results (one for each thread)
results.append(result)
# Start the thread to execute the calculation
thread.start()
# Wait for all threads to complete
for thread in threads:
thread.join()
# Calculate the total sum
total = sum(sum(result) for result in results)
print("Total of sum:", total)
# Get the end time
end_time = time.time()
print("Elapsed time:", end_time - start_time, "seconds")