-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
154 lines (119 loc) · 4.66 KB
/
example.cpp
File metadata and controls
154 lines (119 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* tieredsort usage examples
*/
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <algorithm>
#include "include/tieredsort.hpp"
using namespace std;
using namespace std::chrono;
int main() {
cout << "=== tieredsort Examples ===" << endl << endl;
// Example 1: Basic usage with vector
{
cout << "1. Basic usage:" << endl;
vector<int> data = {5, 2, 8, 1, 9, 3, 7, 4, 6};
cout << " Before: ";
for (int x : data) cout << x << " ";
cout << endl;
tiered::sort(data.begin(), data.end());
cout << " After: ";
for (int x : data) cout << x << " ";
cout << endl << endl;
}
// Example 2: Different types
{
cout << "2. Different types:" << endl;
// 64-bit integers
vector<int64_t> big_ints = {1000000000000LL, -500000000000LL, 999999999999LL};
tiered::sort(big_ints.begin(), big_ints.end());
cout << " int64_t: " << big_ints[0] << ", " << big_ints[1] << ", " << big_ints[2] << endl;
// Floats
vector<float> floats = {3.14f, -2.71f, 1.41f, 0.0f, -0.5f};
tiered::sort(floats.begin(), floats.end());
cout << " float: ";
for (float x : floats) cout << x << " ";
cout << endl;
// Doubles
vector<double> doubles = {3.14159, -2.71828, 1.41421};
tiered::sort(doubles.begin(), doubles.end());
cout << " double: ";
for (double x : doubles) cout << x << " ";
cout << endl << endl;
}
// Example 3: Pre-allocated buffer (zero allocation during sort)
{
cout << "3. Zero-allocation with buffer:" << endl;
vector<int> data = {5, 2, 8, 1, 9};
vector<int> buffer(data.size()); // Reusable buffer
tiered::sort(data.begin(), data.end(), buffer.data());
cout << " Sorted: ";
for (int x : data) cout << x << " ";
cout << endl << endl;
}
// Example 4: Performance comparison
{
cout << "4. Performance comparison (n=100,000):" << endl;
mt19937 rng(42);
uniform_int_distribution<int> dist(0, 1000000);
vector<int> original(100000);
for (int& x : original) x = dist(rng);
vector<int> data = original;
// std::sort
auto start = high_resolution_clock::now();
sort(data.begin(), data.end());
auto end = high_resolution_clock::now();
double t_std = duration<double, micro>(end - start).count();
// tieredsort
data = original;
start = high_resolution_clock::now();
tiered::sort(data.begin(), data.end());
end = high_resolution_clock::now();
double t_tiered = duration<double, micro>(end - start).count();
printf(" std::sort: %.0f μs\n", t_std);
printf(" tieredsort: %.0f μs (%.1fx faster)\n", t_tiered, t_std / t_tiered);
cout << endl;
}
// Example 5: Dense data (counting sort kicks in)
{
cout << "5. Dense data (ages 0-100):" << endl;
mt19937 rng(42);
uniform_int_distribution<int> dist(0, 100);
vector<int> ages(100000);
for (int& x : ages) x = dist(rng);
vector<int> data = ages;
auto start = high_resolution_clock::now();
sort(data.begin(), data.end());
auto end = high_resolution_clock::now();
double t_std = duration<double, micro>(end - start).count();
data = ages;
start = high_resolution_clock::now();
tiered::sort(data.begin(), data.end());
end = high_resolution_clock::now();
double t_tiered = duration<double, micro>(end - start).count();
printf(" std::sort: %.0f μs\n", t_std);
printf(" tieredsort: %.0f μs (%.1fx faster) ← counting sort!\n", t_tiered, t_std / t_tiered);
cout << endl;
}
// Example 6: Already sorted (pattern detection kicks in)
{
cout << "6. Already sorted data:" << endl;
vector<int> sorted(100000);
for (int i = 0; i < 100000; i++) sorted[i] = i;
vector<int> data = sorted;
auto start = high_resolution_clock::now();
sort(data.begin(), data.end());
auto end = high_resolution_clock::now();
double t_std = duration<double, micro>(end - start).count();
data = sorted;
start = high_resolution_clock::now();
tiered::sort(data.begin(), data.end());
end = high_resolution_clock::now();
double t_tiered = duration<double, micro>(end - start).count();
printf(" std::sort: %.0f μs\n", t_std);
printf(" tieredsort: %.0f μs (%.1fx faster) ← pattern detected!\n", t_tiered, t_std / t_tiered);
}
return 0;
}