Refactor benchmark to use randomized vectors instead of sets

- Replaced std::set with std::vector for sequence handling.
- Added randomization of sequences to avoid ordered input bias.
- Removed unused balance function and related code in bst.cpp and bst.h.
- Fixed bugs in list insertion and search logic.
- Updated plot.py to allow custom y-axis labels and enable log scale for build plots.
This commit is contained in:
2025-05-14 19:42:14 +02:00
parent aae0ce7241
commit 899c844c78
5 changed files with 36 additions and 59 deletions
+18 -8
View File
@@ -2,6 +2,7 @@
#include "bst/bst.h"
#include "list/list.h"
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iostream>
@@ -9,7 +10,7 @@
#include <random>
#include <set>
void measureList(std::set<int> *sequence, FILE *file) {
void measureList(std::vector<int> *sequence, FILE *file) {
float buildTime = 0, searchTime = 0, deleteTime = 0;
for (int i = 0; i < 10; i++) {
@@ -55,7 +56,7 @@ void measureList(std::set<int> *sequence, FILE *file) {
fprintf(file, "List,%f,%f,%f\n", buildTime, searchTime, deleteTime);
}
void measureBST(std::set<int> *sequence, FILE *file) {
void measureBST(std::vector<int> *sequence, FILE *file) {
float buildTime = 0, searchTime = 0, deleteTime = 0;
for (int i = 0; i < 10; i++) {
@@ -68,7 +69,7 @@ void measureBST(std::set<int> *sequence, FILE *file) {
for (int value : *sequence) {
root = insert(root, value);
}
root = balance(root);
// root = balance(root);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
buildTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
@@ -100,7 +101,7 @@ void measureBST(std::set<int> *sequence, FILE *file) {
fprintf(file, "BST,%f,%f,%f\n", buildTime, searchTime, deleteTime);
}
void benchmarkAVL(std::set<int> *sequence, FILE *file) {
void benchmarkAVL(std::vector<int> *sequence, FILE *file) {
Tree *bst = nullptr;
for (int value : *sequence) {
bst = insert(bst, value);
@@ -147,19 +148,28 @@ int main() {
}
for (int n = 1; n < 26; n++) {
// Using a set here ensures that there are no duplicates
std::set<int> sequence;
while (sequence.size() < n * 1000) {
sequence.insert(dis(gen));
}
// Display this like a cascade
std::vector<int> random_sequence_vec;
for (int val : sequence) {
random_sequence_vec.push_back(val);
}
// Then randomize the sequence so the bst isn't a list (Set keeps the elements in order)
std::shuffle(random_sequence_vec.begin(), random_sequence_vec.end(), gen);
// Display the times like a cascade
std::cout << "Running tests for " << n * 1000 << " elements..."
<< std::endl;
if (mode == 1) {
measureList(&sequence, file);
measureBST(&sequence, file);
measureList(&random_sequence_vec, file);
measureBST(&random_sequence_vec, file);
} else {
benchmarkAVL(&sequence, file);
benchmarkAVL(&random_sequence_vec, file);
}
}