init
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import subprocess
|
||||
from random import randint
|
||||
from time import time, sleep
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def run_benchmark(program_name, array):
|
||||
start = time()
|
||||
process = subprocess.Popen(program_name, stdin=subprocess.PIPE)
|
||||
process.communicate((f"{len(array)}\n" + "\n".join(map(str, array))).encode())
|
||||
end = time()
|
||||
return end - start
|
||||
|
||||
def generate_sequence(sequence_type, array_size):
|
||||
if sequence_type == "random":
|
||||
return [randint(0, 100) for i in range(array_size)]
|
||||
elif sequence_type == "sorted":
|
||||
return [i for i in range(array_size)]
|
||||
elif sequence_type == "reversed":
|
||||
return [i for i in range(array_size, 0, -1)]
|
||||
elif sequence_type == "constant":
|
||||
return [5 for i in range(array_size)]
|
||||
elif sequence_type == "v_shaped":
|
||||
return [i for i in range(array_size//2, 0, -1)]+[i for i in range(array_size//2)]
|
||||
else:
|
||||
raise ValueError("Invalid sequence type")
|
||||
|
||||
def plot(program_name, sequences, array_size):
|
||||
plt.figure(figsize=(10, 6))
|
||||
for sequence_type, timings in sequences.items():
|
||||
plt.plot(array_size, timings, label=sequence_type)
|
||||
plt.xlabel("")
|
||||
plt.ylabel("Czas (ms)")
|
||||
plt.title(f"{program_name}")
|
||||
plt.legend()
|
||||
plt.savefig(f"benchmarks/{program_name}.png")
|
||||
# plt.show()
|
||||
|
||||
if __name__ == "__main__":
|
||||
program_names = ["./insertion", "./selection"]
|
||||
sequence_types = ["random", "sorted", "reversed", "constant", "v_shaped"]
|
||||
array_sizes = [i for i in range(1000, 15001, 1000)]
|
||||
|
||||
with open("results.txt", "w") as f:
|
||||
for program_name in program_names:
|
||||
sequenceTiming = {
|
||||
"random": [],
|
||||
"sorted": [],
|
||||
"reversed": [],
|
||||
"constant": [],
|
||||
"v_shaped": []
|
||||
}
|
||||
print(f"Running {program_name}")
|
||||
for sequence_type in sequence_types:
|
||||
print(f" - running {sequence_type}")
|
||||
for array_size in array_sizes:
|
||||
sequence = generate_sequence(sequence_type, array_size)
|
||||
print(f" - running {sequence_type} with size {array_size}")
|
||||
durations = []
|
||||
for i in range(10):
|
||||
duration = run_benchmark(program_name, sequence)
|
||||
duration *= 1000
|
||||
durations.append(duration)
|
||||
sleep(0.1)
|
||||
duration = sum(durations) / len(durations)
|
||||
f.write(f"{program_name} {sequence_type} {array_size} {duration}\n")
|
||||
sequenceTiming[sequence_type].append(duration)
|
||||
sleep(0.5)
|
||||
plot(program_name, sequenceTiming, array_sizes)
|
||||
print("Benchmark finished")
|
||||
Reference in New Issue
Block a user