This project evaluates the practical performance of custom data structures and sorting algorithms through a series of performance benchmarks.
The project consists of two independent experiments:
- comparison of associative containers;
- comparison of sorting algorithms.
For associative containers, the implementation compares a custom hash table and a custom red-black tree with their corresponding C++ Standard Library implementations (std::unordered_map and std::map). The following operations are measured:
- insertion;
- successful search;
- unsuccessful search;
- deletion.
For sorting algorithms, the project compares QuickSort and Insertion Sort on several input distributions in order to evaluate how the initial ordering of the data influences execution time.
In addition to reporting measured execution times, the project estimates the hidden constants of the theoretical time complexity using regression analysis based on the collected benchmark results.
.
├── include
│ ├── benchmark
│ │ ├── container_benchmarks.h
│ │ ├── data_generators.h
│ │ ├── helpers.h
│ │ ├── regression.h
│ │ ├── sorting_benchmarks.h
│ │ └── timing.h
│ ├── hash_function.h
│ ├── hash_table.h
│ ├── insertion_sort.h
│ ├── quick_sort.h
│ └── rbtree.h
│
├── src
│ └── main.cpp
│
└── Makefile
The project is divided into two logical parts.
The implementations of the data structures and sorting algorithms are separated from the benchmarking framework. The benchmark module is responsible for generating input data, executing performance measurements, calculating regression coefficients, and presenting the experimental results.
The main program performs all benchmarks automatically and prints the measured execution times together with the estimated hidden constants.
Execution time is measured using the POSIX monotonic clock (CLOCK_MONOTONIC). Unlike the system clock, the monotonic clock is not affected by changes to the system time, providing stable measurements throughout the benchmark.
Each benchmark is executed in two stages.
First, the tested operation is performed two warm-up runs. These executions are not included in the final result and are intended to eliminate one-time initialization effects before the actual measurements begin.
The benchmark is executed twenty times, and the reported execution time is calculated as the arithmetic mean:
Tavg = (T₁ + T₂ + ... + T₂₀) / 20
where:
- T₁ ... T₂₀ are the execution times of individual runs;
- 20 is the number of benchmark executions.
Using the average execution time reduces the influence of random fluctuations caused by the operating system and background processes.
During search benchmarks, the returned value of each lookup is referenced by a minimal inline assembly statement. This prevents the compiler from optimizing away the search operation while introducing virtually no additional computational work, ensuring that the measured execution time corresponds to the actual lookup performed by the tested container.
All benchmark data is generated automatically.
For every tested container size, a sequence of unique integer keys is generated.
The keys are created as consecutive integers and then randomly shuffled using a fixed random seed. Using the same seed guarantees that every implementation is tested with exactly the same dataset.
Successful search operations are performed using the inserted keys in randomized order.
To measure unsuccessful searches, another sequence of keys is generated by adding a large constant offset to every existing key. Since these values were never inserted into the container, every lookup is guaranteed to fail. The generated sequence is also randomly shuffled before the benchmark begins.
Sorting algorithms are evaluated on four different input distributions.
- Random — elements are randomly shuffled.
- Sorted — elements are already arranged in ascending order.
- Reversed — elements are arranged in descending order.
- Nearly sorted — the array is initially sorted, after which approximately 5% of adjacent element pairs are swapped.
Testing several input distributions allows the benchmark to evaluate algorithm performance under different practical scenarios instead of considering only completely random input.
Hidden Constants Estimation
Besides measuring execution time, the project estimates the hidden constants of the theoretical time complexity for the implemented data structures and sorting algorithms.
The estimation is based on the benchmark results obtained for different input sizes. The measured execution times are approximated by mathematical models corresponding to the expected asymptotic complexity of each algorithm. The coefficients of these models are obtained using the least-squares regression method, which minimizes the total squared error between the measured values and the approximation.
The expected complexity of hash table operations is O(1).
For each benchmark, the average execution time of one operation is computed as
t = T / n
where
T– measured execution time;n– number of processed elements.
Since the expected complexity is constant, the hidden constant is simply the average value of t over all benchmark sizes.
Operations on the red-black tree have O(log n) complexity.
For every measurement, the average execution time per operation is first computed as
t = T / n
The experimental data is then approximated by the linear model
t = a · log₂(n) + b
where
ais the hidden constant of the logarithmic term;bis the constant overhead.
The regression minimizes the total squared approximation error
Σ (yi − (a·xi + b))²
where
xi = log₂(ni)
yi = ti
The coefficients are computed using the least-squares formulas
a = (m Σxi yi − Σxi Σyi) / (m Σxi² − (Σxi)²)
b = (Σyi − a Σxi) / m
Separate regression models are constructed for insertion, successful search, unsuccessful search and deletion.
QuickSort has the expected complexity O(n log n).
Its execution time is approximated by
T = a · n · log₂(n) + b
where
ais the hidden constant of the dominant term;bis the constant overhead.
The coefficients are obtained using the same linear least-squares regression procedure, where
x = n · log₂(n)
Insertion Sort has O(n²) complexity.
The measured execution time is approximated by
T = a·n² + b·n + c
where
ais the hidden constant of the quadratic term;bis the coefficient of the linear term;cis the constant overhead.
Applying the least-squares method produces the following system of equations:
s0·c + s1·b + s2·a = t0
s1·c + s2·b + s3·a = t1
s2·c + s3·b + s4·a = t2
where
sk = Σ(xᵏ)
tk = Σ(xᵏ · y)
The resulting 3×3 linear system is solved using Gaussian elimination to obtain the coefficients a, b, and c.
The coefficient a corresponds to the hidden constant of the quadratic complexity, while b and c describe the lower-order terms of the approximation. The resulting 3×3 system of linear equations is solved using Gaussian elimination to obtain the coefficients a, b, and c.
Size: 10000 elements
┌─────────────┬──────────┬──────────┬──────────┬──────────┬────────┐
│ Operation │ HT (ms) │ RB (ms) │ UM (ms) │ MAP (ms) │ Winner │
├─────────────┼──────────┼──────────┼──────────┼──────────┼────────┤
│ Insert │ 2.16 │ 1.38 │ 0.31 │ 0.98 │ UM │
│ Find OK │ 0.02 │ 0.71 │ 0.02 │ 0.74 │ UM │
│ Find Fail │ 0.05 │ 0.05 │ 0.06 │ 0.17 │ RB │
│ Erase │ 1.00 │ 2.15 │ 0.32 │ 1.86 │ UM │
└─────────────┴──────────┴──────────┴──────────┴──────────┴────────┘
Result Analysis
This table compares the execution time of insert, search, and erase operations for all containers using 10,000 elements. std::unordered_map is the fastest for most operations because of its highly optimized hash table implementation. The custom HashTable achieves almost identical lookup performance but is slower for insertion and deletion due to implementation overhead. The tree-based containers are slower for successful searches because they must traverse a balanced tree. The red-black tree performs slightly better for unsuccessful searches, since the search simply terminates after reaching a leaf without examining hash buckets.
Size: 50000 elements
┌─────────────┬──────────┬──────────┬──────────┬──────────┬────────┐
│ Operation │ HT (ms) │ RB (ms) │ UM (ms) │ MAP (ms) │ Winner │
├─────────────┼──────────┼──────────┼──────────┼──────────┼────────┤
│ Insert │ 4.81 │ 11.85 │ 2.66 │ 8.04 │ UM │
│ Find OK │ 0.22 │ 7.20 │ 0.16 │ 7.45 │ UM │
│ Find Fail │ 0.48 │ 0.37 │ 0.41 │ 0.47 │ RB │
│ Erase │ 8.47 │ 17.30 │ 3.28 │ 17.40 │ UM │
└─────────────┴──────────┴──────────┴──────────┴──────────┴────────┘
Result Analysis
This table shows that the difference between hash tables and balanced trees becomes more noticeable as the number of elements increases. Both hash tables still perform insertions, successful searches, and deletions much faster than the tree implementations because these operations remain close to constant time on average. The red-black tree again performs best for unsuccessful searches, while the custom HashTable remains slower than std::unordered_map because the standard library implementation is more optimized.
Size: 100000 elements
┌─────────────┬──────────┬──────────┬──────────┬──────────┬────────┐
│ Operation │ HT (ms) │ RB (ms) │ UM (ms) │ MAP (ms) │ Winner │
├─────────────┼──────────┼──────────┼──────────┼──────────┼────────┤
│ Insert │ 14.83 │ 26.26 │ 6.09 │ 17.86 │ UM │
│ Find OK │ 0.78 │ 18.58 │ 0.43 │ 17.65 │ UM │
│ Find Fail │ 0.97 │ 0.77 │ 0.82 │ 0.91 │ RB │
│ Erase │ 12.55 │ 39.02 │ 7.07 │ 34.01 │ UM │
└─────────────┴──────────┴──────────┴──────────┴──────────┴────────┘
Result Analysis
The same performance trends continue for 100,000 elements. Hash-based containers remain significantly faster for insertion, successful lookup, and deletion because their average complexity is constant. The running time of the tree-based containers increases with the tree height, making them noticeably slower. Failed searches remain the only operation where the red-black tree has the lowest measured time.
Size: 500000 elements
┌─────────────┬──────────┬──────────┬──────────┬──────────┬────────┐
│ Operation │ HT (ms) │ RB (ms) │ UM (ms) │ MAP (ms) │ Winner │
├─────────────┼──────────┼──────────┼──────────┼──────────┼────────┤
│ Insert │ 80.82 │ 272.41 │ 62.05 │ 255.82 │ UM │
│ Find OK │ 10.63 │ 223.83 │ 11.44 │ 347.59 │ HT │
│ Find Fail │ 10.82 │ 5.80 │ 14.35 │ 7.26 │ RB │
│ Erase │ 100.37 │ 508.69 │ 98.77 │ 481.04 │ UM │
└─────────────┴──────────┴──────────┴──────────┴──────────┴────────┘
Result Analysis
This table demonstrates the behavior on the largest tested dataset. std::unordered_map remains the fastest for insertion and deletion, while the custom HashTable slightly outperforms it during successful searches. This difference is small and most likely caused by implementation details and measurement noise. The tree implementations become much slower because every operation still requires traversing a balanced tree with many levels. As in the previous tests, unsuccessful searches are completed fastest by the red-black tree.
Size: 1000 elements
┌─────────────────┬─────────────────┬─────────────────┬───────────────┐
│ Input type │ QuickSort (ms) │ InsertSort (ms) │ Speedup (QS) │
├─────────────────┼─────────────────┼─────────────────┼───────────────┤
│ Random │ 0.01 │ 0.07 │ 5.98 │
│ Sorted │ 0.01 │ 0.00 │ 0.13 │
│ Reversed │ 0.02 │ 0.14 │ 8.03 │
│ Nearly sorted │ 0.01 │ 0.00 │ 0.18 │
└─────────────────┴─────────────────┴─────────────────┴───────────────┘
Result Analysis
The table compares QuickSort and Insertion Sort for different input orders. QuickSort is already faster on random and reversed data. However, Insertion Sort performs better on sorted and nearly sorted arrays because it requires almost no element shifts.
Size: 2000 elements
┌─────────────────┬─────────────────┬─────────────────┬───────────────┐
│ Input type │ QuickSort (ms) │ InsertSort (ms) │ Speedup (QS) │
├─────────────────┼─────────────────┼─────────────────┼───────────────┤
│ Random │ 0.07 │ 0.32 │ 4.74 │
│ Sorted │ 0.01 │ 0.00 │ 0.12 │
│ Reversed │ 0.02 │ 0.62 │ 40.00 │
│ Nearly sorted │ 0.01 │ 0.00 │ 0.17 │
└─────────────────┴─────────────────┴─────────────────┴───────────────┘
Result Analysis
As the input size increases, QuickSort becomes noticeably faster on random and reversed arrays. Insertion Sort still remains the best choice for sorted and nearly sorted data because its running time is close to linear in these cases.
Size: 5000 elements
┌─────────────────┬─────────────────┬─────────────────┬───────────────┐
│ Input type │ QuickSort (ms) │ InsertSort (ms) │ Speedup (QS) │
├─────────────────┼─────────────────┼─────────────────┼───────────────┤
│ Random │ 0.27 │ 1.87 │ 6.90 │
│ Sorted │ 0.03 │ 0.00 │ 0.13 │
│ Reversed │ 0.03 │ 3.65 │ 117.58 │
│ Nearly sorted │ 0.03 │ 0.01 │ 0.17 │
└─────────────────┴─────────────────┴─────────────────┴───────────────┘
Result Analysis
The gap between the algorithms continues to grow. QuickSort benefits from its O(n log n) complexity on random data, while Insertion Sort becomes increasingly slow on reversed arrays due to its quadratic complexity.
Size: 10000 elements
┌─────────────────┬─────────────────┬─────────────────┬───────────────┐
│ Input type │ QuickSort (ms) │ InsertSort (ms) │ Speedup (QS) │
├─────────────────┼─────────────────┼─────────────────┼───────────────┤
│ Random │ 0.50 │ 6.51 │ 12.91 │
│ Sorted │ 0.06 │ 0.01 │ 0.12 │
│ Reversed │ 0.10 │ 13.86 │ 144.38 │
│ Nearly sorted │ 0.06 │ 0.01 │ 0.16 │
└─────────────────┴─────────────────┴─────────────────┴───────────────┘
Result Analysis
QuickSort clearly outperforms Insertion Sort on random and reversed inputs. The advantage of Insertion Sort is preserved only for already sorted or almost sorted arrays, where very few element movements are required.
Size: 20000 elements
┌─────────────────┬─────────────────┬─────────────────┬───────────────┐
│ Input type │ QuickSort (ms) │ InsertSort (ms) │ Speedup (QS) │
├─────────────────┼─────────────────┼─────────────────┼───────────────┤
│ Random │ 1.39 │ 30.00 │ 21.59 │
│ Sorted │ 0.17 │ 0.01 │ 0.09 │
│ Reversed │ 0.14 │ 51.54 │ 376.07 │
│ Nearly sorted │ 0.13 │ 0.02 │ 0.16 │
└─────────────────┴─────────────────┴─────────────────┴───────────────┘
Result Analysis
With larger arrays, the quadratic complexity of Insertion Sort becomes much more noticeable. QuickSort scales significantly better, while Insertion Sort remains efficient only when the array is already almost ordered.
Size: 50000 elements
┌─────────────────┬─────────────────┬─────────────────┬───────────────┐
│ Input type │ QuickSort (ms) │ InsertSort (ms) │ Speedup (QS) │
├─────────────────┼─────────────────┼─────────────────┼───────────────┤
│ Random │ 3.71 │ 162.81 │ 43.88 │
│ Sorted │ 0.31 │ 0.04 │ 0.12 │
│ Reversed │ 0.47 │ 324.07 │ 689.68 │
│ Nearly sorted │ 0.69 │ 0.10 │ 0.14 │
└─────────────────┴─────────────────┴─────────────────┴───────────────┘
Result Analysis
QuickSort continues to scale well as the input size increases. Insertion Sort becomes hundreds of times slower on reversed arrays because every new element must be shifted through almost the entire sorted part of the array.
Size: 100000 elements
┌─────────────────┬─────────────────┬─────────────────┬───────────────┐
│ Input type │ QuickSort (ms) │ InsertSort (ms) │ Speedup (QS) │
├─────────────────┼─────────────────┼─────────────────┼───────────────┤
│ Random │ 6.21 │ 631.64 │ 101.74 │
│ Sorted │ 0.97 │ 0.07 │ 0.07 │
│ Reversed │ 0.85 │ 1292.84 │ 1518.27 │
│ Nearly sorted │ 1.00 │ 0.10 │ 0.10 │
└─────────────────┴─────────────────┴─────────────────┴───────────────┘
Result Analysis
For large datasets, QuickSort is over one hundred times faster on random data and more than one thousand times faster on reversed data. Insertion Sort still shows the best performance only for sorted and nearly sorted inputs.
Size: 200000 elements
┌─────────────────┬─────────────────┬─────────────────┬───────────────┐
│ Input type │ QuickSort (ms) │ InsertSort (ms) │ Speedup (QS) │
├─────────────────┼─────────────────┼─────────────────┼───────────────┤
│ Random │ 12.61 │ 2463.59 │ 195.31 │
│ Sorted │ 1.74 │ 0.13 │ 0.08 │
│ Reversed │ 1.59 │ 5271.04 │ 3324.99 │
│ Nearly sorted │ 1.78 │ 0.28 │ 0.16 │
└─────────────────┴─────────────────┴─────────────────┴───────────────┘
Result Analysis
The largest test clearly illustrates the difference in algorithmic complexity. QuickSort maintains good scalability, whereas Insertion Sort becomes impractical for random and reversed arrays because of its quadratic running time. For sorted and nearly sorted inputs, Insertion Sort remains the faster algorithm due to the minimal number of required shifts.
Hidden Constants
Hidden constants (regression coefficients, in ms)
HashTable (average time per operation, in ms):
Insert: 0.000106394275 ms
Find OK: 0.000008534764 ms
Find Fail:0.000010970790 ms
Erase: 0.000122765195 ms
RBTree (t per op = a * log2(n) + b):
Insert: a = 0.000048675080 ms, b = -0.000526568637 ms
Find OK: a = 0.000052892932 ms, b = -0.000679346690 ms
Find Fail:a = 0.000000796023 ms, b = -0.000005971104 ms
Erase: a = 0.000081117282 ms, b = -0.000928047751 ms
QuickSort (t = a * n*log2(n) + b):
a = 0.000003972075 ms, b = -0.068980327653 ms
InsertionSort (t = a * n² + b * n + c):
a = 0.000000064355 ms
b = -0.000228568492 ms
c = 0.138248443315 ms
Result Analysis
This table presents the hidden constants obtained by fitting the measured execution times to their theoretical complexity functions. For the hash table, the values represent the average execution time of a single operation because its expected complexity is O(1). For the red-black tree, coefficient a determines how quickly the running time grows with log₂(n), while b represents fixed overhead independent of the input size. For QuickSort, coefficient a corresponds to the constant in the O(n log n) term, confirming that the measured times closely follow the expected complexity. For Insertion Sort, the dominant coefficient a belongs to the quadratic term n², explaining why its running time increases rapidly for large random and reversed inputs, while the remaining coefficients account for lower-order effects and constant overhead. The negative value of b is a result of the least-squares approximation: it compensates for lower-order effects and measurement noise to provide the best fit to the experimental data, and does not imply a negative execution time. Coefficient c represents the constant overhead of the approximation.
Use the provided Makefile:
makeThis command compiles the demonstration program together with all benchmark components and produces the executable file:
./main