mirror of
https://github.com/RustyRaptor/parallel-programming.git
synced 2026-12-03 23:55:23 -07:00
71 KiB
71 KiB
In [1]:
import matplotlib.pyplot as plt
import numpy as np
# Provided program output as a string
program_output = """
1 threads
Number of threes: 9997807
Time taken: 0.359876473 seconds
Number of threes: 9998567
Time taken: 0.361032238 seconds
Number of threes: 9996883
Time taken: 0.360004483 seconds
Number of threes: 9996455
Time taken: 0.359855544 seconds
Number of threes: 9998718
Time taken: 0.359787787 seconds
2 threads
Number of threes: 9998562
Time taken: 0.373749715 seconds
Number of threes: 10004232
Time taken: 0.447702756 seconds
Number of threes: 10000537
Time taken: 0.464235889 seconds
Number of threes: 9997416
Time taken: 0.372059609 seconds
Number of threes: 9996787
Time taken: 0.371326817 seconds
4 threads
Number of threes: 10004804
Time taken: 0.222983097 seconds
Number of threes: 9997107
Time taken: 0.216992067 seconds
Number of threes: 10000709
Time taken: 0.21719135 seconds
Number of threes: 9996199
Time taken: 0.21907073 seconds
Number of threes: 10008067
Time taken: 0.22178881 seconds
8 threads
Number of threes: 10006502
Time taken: 0.167658194 seconds
Number of threes: 10004407
Time taken: 0.165304926 seconds
Number of threes: 9997402
Time taken: 0.163078056 seconds
Number of threes: 9999915
Time taken: 0.162809502 seconds
Number of threes: 9999852
Time taken: 0.165746493 seconds
16 threads
Number of threes: 9997128
Time taken: 0.178607489 seconds
Number of threes: 9995240
Time taken: 0.172914077 seconds
Number of threes: 10001920
Time taken: 0.174337312 seconds
Number of threes: 10000932
Time taken: 0.176571896 seconds
Number of threes: 10005193
Time taken: 0.175561715 seconds
32 threads
Number of threes: 9998869
Time taken: 0.190420432 seconds
Number of threes: 10000228
Time taken: 0.190276435 seconds
Number of threes: 9372724
Time taken: 0.178388981 seconds
Number of threes: 10000496
Time taken: 0.180038991 seconds
Number of threes: 9997442
Time taken: 0.149067537 seconds
64 threads
Number of threes: 9687065
Time taken: 0.160374063 seconds
Number of threes: 9996783
Time taken: 0.229213367 seconds
Number of threes: 9997292
Time taken: 0.282740373 seconds
Number of threes: 9530897
Time taken: 0.19328029 seconds
Number of threes: 9689583
Time taken: 0.199149752 seconds
"""
# Split the output into lines
lines = program_output.strip().split("\n")
# Initialize lists to hold the times for each thread count
times_1_thread = []
times_2_threads = []
times_4_threads = []
times_8_threads = []
times_16_threads = []
times_32_threads = []
times_64_threads = []
# Initialize a list of lists to make it easier to append in a loop
all_times = [
times_1_thread,
times_2_threads,
times_4_threads,
times_8_threads,
times_16_threads,
times_32_threads,
times_64_threads,
]
# Current thread count index
thread_count_index = 0
# Parse the output and fill the lists
for line in lines:
if "Time taken:" in line:
# Extract the time taken from the line
time_taken = float(line.split()[-2])
# Append the time to the appropriate list
all_times[thread_count_index].append(time_taken)
# Check if the current thread count's list is filled with 5 runs
if len(all_times[thread_count_index]) == 5:
# Move to the next thread count
thread_count_index += 1
# Now all_times contains the lists of times for each thread count
print("Times for 1 thread:", times_1_thread)
print("Times for 2 threads:", times_2_threads)
print("Times for 4 threads:", times_4_threads)
print("Times for 8 threads:", times_8_threads)
print("Times for 16 threads:", times_16_threads)
print("Times for 32 threads:", times_32_threads)
print("Times for 64 threads:", times_64_threads)
Times for 1 thread: [0.359876473, 0.361032238, 0.360004483, 0.359855544, 0.359787787] Times for 2 threads: [0.373749715, 0.447702756, 0.464235889, 0.372059609, 0.371326817] Times for 4 threads: [0.222983097, 0.216992067, 0.21719135, 0.21907073, 0.22178881] Times for 8 threads: [0.167658194, 0.165304926, 0.163078056, 0.162809502, 0.165746493] Times for 16 threads: [0.178607489, 0.172914077, 0.174337312, 0.176571896, 0.175561715] Times for 32 threads: [0.190420432, 0.190276435, 0.178388981, 0.180038991, 0.149067537] Times for 64 threads: [0.160374063, 0.229213367, 0.282740373, 0.19328029, 0.199149752]
In [2]:
# Assuming the previous code has been executed and all_times is populated
# Calculate the average time for each thread count
average_times = [sum(times) / len(times) for times in all_times]
# max_threads = 64
# Thread counts
thread_counts = [1,2,4,8,16,32,64]
# Plotting the running time bar graph
plt.figure(figsize=(7, 7))
plt.plot(thread_counts, average_times, color='skyblue', )
# Adding titles and labels
plt.title('Average runtime by Thread Count (Java Threads)')
plt.xlabel('Number of Threads')
plt.ylabel('Running time (s)')
plt.xticks(thread_counts)
# plt.xscale('log', base=2)
# Display the bar graph
plt.show()In [3]:
# Calculate speedup compared to single-threaded execution
speedup = [average_times[0] / time for time in average_times]
# Plotting the speedup bar graph
plt.figure(figsize=(10, 7))
plt.bar(thread_counts, speedup, color='skyblue')
# Adding titles and labels
plt.title('Speedup by Thread Count (Java Threads)')
plt.xlabel('Number of Threads')
plt.ylabel('Speedup Factor')
plt.xticks(thread_counts)
# Display the bar graph
plt.show()In [ ]: