Added avl height benchmark
This commit is contained in:
+126
-82
@@ -1,97 +1,123 @@
|
||||
#include "avl/avl.h"
|
||||
#include "bst/bst.h"
|
||||
#include "list/list.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <ostream>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <set>
|
||||
|
||||
void measureList(std::set<int> *sequence, FILE *file){
|
||||
float buildTime = 0, searchTime = 0, deleteTime = 0;
|
||||
void measureList(std::set<int> *sequence, FILE *file) {
|
||||
float buildTime = 0, searchTime = 0, deleteTime = 0;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
std::cout << "- Running list " << i + 1 << "/10\r";
|
||||
fflush(stdout);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
std::cout << "- Running list " << i + 1 << "/10\r";
|
||||
fflush(stdout);
|
||||
|
||||
// Measure build time
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
List *head = nullptr;
|
||||
for (int value : *sequence) {
|
||||
head = insert(head, value);
|
||||
}
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> elapsed_seconds = end - start;
|
||||
buildTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
|
||||
// Now we run the search
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
for (int value : *sequence) {
|
||||
search(head, value);
|
||||
}
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
searchTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
|
||||
// Measure deletion time for list
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
while(head != nullptr){
|
||||
head = remove(head);
|
||||
}
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
deleteTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
// Measure build time
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
List *head = nullptr;
|
||||
for (int value : *sequence) {
|
||||
head = insert(head, value);
|
||||
}
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> elapsed_seconds = end - start;
|
||||
buildTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
|
||||
buildTime /= 10;
|
||||
searchTime /= 10;
|
||||
deleteTime /= 10;
|
||||
// Now we run the search
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
for (int value : *sequence) {
|
||||
search(head, value);
|
||||
}
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
searchTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
|
||||
std::cout << "- List built in " << buildTime << "ms | searched in " << searchTime << "ms | deleted in " << deleteTime << "ms" << std::endl;
|
||||
fprintf(file, "List,%f,%f,%f\n", buildTime, searchTime, deleteTime);
|
||||
// Measure deletion time for list
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
while (head != nullptr) {
|
||||
head = remove(head);
|
||||
}
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
deleteTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
}
|
||||
|
||||
buildTime /= 10;
|
||||
searchTime /= 10;
|
||||
deleteTime /= 10;
|
||||
|
||||
std::cout << "- List built in " << buildTime << "ms | searched in "
|
||||
<< searchTime << "ms | deleted in " << deleteTime << "ms"
|
||||
<< std::endl;
|
||||
fprintf(file, "List,%f,%f,%f\n", buildTime, searchTime, deleteTime);
|
||||
}
|
||||
|
||||
void measureBST(std::set<int> *sequence, FILE *file){
|
||||
float buildTime = 0, searchTime = 0, deleteTime = 0;
|
||||
void measureBST(std::set<int> *sequence, FILE *file) {
|
||||
float buildTime = 0, searchTime = 0, deleteTime = 0;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
std::cout << "- Running bst " << i + 1 << "/10\r";
|
||||
fflush(stdout);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
std::cout << "- Running bst " << i + 1 << "/10\r";
|
||||
fflush(stdout);
|
||||
|
||||
// Measure build time
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
Tree *root = nullptr;
|
||||
for (int value : *sequence) {
|
||||
root = insert(root, value);
|
||||
}
|
||||
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
|
||||
|
||||
// Now we run the search
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
for (int value : *sequence) {
|
||||
search(root, value);
|
||||
}
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
searchTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
|
||||
// Measure deletion time for tree
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
deleteTree(root);
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
deleteTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
// Measure build time
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
Tree *root = nullptr;
|
||||
for (int value : *sequence) {
|
||||
root = insert(root, value);
|
||||
}
|
||||
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
|
||||
|
||||
buildTime /= 10;
|
||||
searchTime /= 10;
|
||||
deleteTime /= 10;
|
||||
// Now we run the search
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
for (int value : *sequence) {
|
||||
search(root, value);
|
||||
}
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
searchTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
|
||||
std::cout << "- Tree built in " << buildTime << "ms | searched in " << searchTime << "ms | deleted in " << deleteTime << "ms" << std::endl;
|
||||
fprintf(file, "BST,%f,%f,%f\n", buildTime, searchTime, deleteTime);
|
||||
// Measure deletion time for tree
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
deleteTree(root);
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
deleteTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
|
||||
}
|
||||
|
||||
buildTime /= 10;
|
||||
searchTime /= 10;
|
||||
deleteTime /= 10;
|
||||
|
||||
std::cout << "- Tree built in " << buildTime << "ms | searched in "
|
||||
<< searchTime << "ms | deleted in " << deleteTime << "ms"
|
||||
<< std::endl;
|
||||
fprintf(file, "BST,%f,%f,%f\n", buildTime, searchTime, deleteTime);
|
||||
}
|
||||
|
||||
void benchmarkAVL(std::set<int> *sequence, FILE *file) {
|
||||
Tree *bst = nullptr;
|
||||
for (int value : *sequence) {
|
||||
bst = insert(bst, value);
|
||||
}
|
||||
|
||||
int heightBST = getHeight(bst, 0);
|
||||
deleteTree(bst);
|
||||
|
||||
AVL_tree *avl = nullptr;
|
||||
for (int value : *sequence) {
|
||||
avl = insertAVL(avl, value);
|
||||
}
|
||||
|
||||
int heightAVL = getHeight(avl);
|
||||
deleteAVL(avl);
|
||||
|
||||
fprintf(file, "%d,%d\n", heightBST, heightAVL);
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -100,9 +126,25 @@ int main() {
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(1, 1000000);
|
||||
|
||||
// Open a file for writing results
|
||||
FILE *file = fopen("results.csv", "w");
|
||||
fprintf(file, "Elements,Structure,BuildTime,SearchTime,DeleteTime\n");
|
||||
int mode = 1;
|
||||
std::cout << "Select benchmark to run:\n 1 - Benchmark List vs BST\n 2 - "
|
||||
"Benchmark BST vs AVL"
|
||||
<< std::endl;
|
||||
std::cin >> mode;
|
||||
|
||||
if (mode < 1 && mode > 2) {
|
||||
mode = 1;
|
||||
}
|
||||
|
||||
FILE *file;
|
||||
if (mode == 1) {
|
||||
// Open a file for writing results
|
||||
file = fopen("results.csv", "w");
|
||||
fprintf(file, "Structure,BuildTime,SearchTime,DeleteTime\n");
|
||||
} else {
|
||||
file = fopen("avl.csv", "w");
|
||||
std::fprintf(file, "Structure,Height\n");
|
||||
}
|
||||
|
||||
for (int n = 1; n < 26; n++) {
|
||||
std::set<int> sequence;
|
||||
@@ -113,12 +155,14 @@ int main() {
|
||||
// Display this like a cascade
|
||||
std::cout << "Running tests for " << n * 1000 << " elements..."
|
||||
<< std::endl;
|
||||
|
||||
measureList(&sequence, file);
|
||||
measureBST(&sequence, file);
|
||||
if (mode == 1) {
|
||||
measureList(&sequence, file);
|
||||
measureBST(&sequence, file);
|
||||
} else {
|
||||
benchmarkAVL(&sequence, file);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user