I was comparing BST to Heap at: Heap vs Binary Search Tree (BST) but when I tried to benchmark both and compare results, I could not interpret the data for BST.
First, I
The clock is likely not accurate enough as mentioned in the comments, so I've tried to group a bunch of inserts together and time them to improve the signal to noise, and it worked, I can see a logarithm now:
#include <chrono>
#include <iostream>
#include <random>
#include <set>
int main(int argc, char **argv) {
size_t i, j, n, granule;
std::set<int> bst;
std::random_device dev;
unsigned int seed = dev();
std::mt19937 prng(seed);
std::uniform_int_distribution<> dist;
int *randoms;
if (argc > 1) {
n = std::stoi(argv[1]);
} else {
n = 1000000;
}
if (argc > 2) {
granule = std::stoi(argv[2]);
} else {
granule = 10;
}
randoms = new int[granule];
for (i = 0; i < n / granule; ++i) {
for (j = 0; j < granule; ++j) {
randoms[j] = dist(prng);
}
auto start = std::chrono::high_resolution_clock::now();
for (j = 0; j < granule; ++j) {
bst.insert(randoms[j]);
}
auto end = std::chrono::high_resolution_clock::now();
auto dt_bst = end - start;
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(dt_bst).count() << std::endl;
}
delete[] randoms;
}
Command:
./main.out 100000000 10000
Graph: