Browse Source

添加 "随机key例子和有序key例子"

ToiletMaster 10 months ago
parent
commit
30b7b76414

+ 26 - 24
analysis3.cpp → example/order/construct_example.cpp

@@ -12,36 +12,47 @@ const int M1 = 3;
 const int M2 = 5;
 
 struct Test {
-	int num;
-	string str;
+	int a;
+	short b;
+	int c;
 };
 
 vector<long long> umap_times;
 vector<long long> tire_times;
 
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		string str = to_string(i + 1);
+		data_table.push_back(str);
+	}
+}
+
 void umap_test() {
+	const int n = data_table.size();
 	auto start = chrono::high_resolution_clock::now();
 	unordered_map<string, Test> umap;
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		umap.insert({ test.str, test });
-	};
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
 	auto end = chrono::high_resolution_clock::now();
 	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
 	umap_times.push_back(duration.count());
 }
 
 void tire_test() {
+	const int n = data_table.size();
 	auto start = chrono::high_resolution_clock::now();
 	Tire<Test> tire;
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		tire.insert(test.str, test);
-	};
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
 	auto end = chrono::high_resolution_clock::now();
 	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
 	tire_times.push_back(duration.count());
@@ -59,15 +70,8 @@ void analysis(string name, vector<long long>& nums) {
 	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
 }
 
-void print_times(string name, vector<long long>& nums) {
-	cout << name << endl;
-	for (long long& num : nums) {
-		cout << num << " ";
-	};
-	cout << endl;
-}
-
 int main() {
+	init_data_table();
 	// group 1
 	for (int i = 0; i < M1; ++i) {
 		umap_test();
@@ -97,8 +101,6 @@ int main() {
 	}
 
 	// 分析
-	print_times("umap", umap_times);
-	print_times("tire", tire_times);
 	analysis("umap", umap_times);
 	analysis("tire", tire_times);
 	return 0;

+ 29 - 27
analysis4.cpp → example/order/destruct_example.cpp

@@ -12,31 +12,31 @@ const int M1 = 3;
 const int M2 = 5;
 
 struct Test {
-	int num;
-	string str;
+	int a;
+	short b;
+	int c;
 };
 
 vector<long long> umap_times;
 vector<long long> tire_times;
 
-void _umap_test() {
-	unordered_map<string, Test> umap;
+vector<string> data_table;
+void init_data_table() {
 	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		umap.insert({ test.str, test });
-	};
+		string str = to_string(i + 1);
+		data_table.push_back(str);
+	}
 }
 
-void _tire_test() {
-	Tire<Test> tire;
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		tire.insert(test.str, test);
-	};
+void _umap_test() {
+	const int n = data_table.size();
+	unordered_map<string, Test> umap;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
 }
 
 void umap_test() {
@@ -47,6 +47,17 @@ void umap_test() {
 	umap_times.push_back(duration.count());
 }
 
+void _tire_test() {
+	const int n = data_table.size();
+	Tire<Test> tire;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+}
+
 void tire_test() {
 	auto start = chrono::high_resolution_clock::now();
 	_tire_test();
@@ -67,15 +78,8 @@ void analysis(string name, vector<long long>& nums) {
 	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
 }
 
-void print_times(string name, vector<long long>& nums) {
-	cout << name << endl;
-	for (long long& num : nums) {
-		cout << num << " ";
-	};
-	cout << endl;
-}
-
 int main() {
+	init_data_table();
 	// group 1
 	for (int i = 0; i < M1; ++i) {
 		umap_test();
@@ -105,8 +109,6 @@ int main() {
 	}
 
 	// 分析
-	print_times("umap", umap_times);
-	print_times("tire", tire_times);
 	analysis("umap", umap_times);
 	analysis("tire", tire_times);
 	return 0;

+ 128 - 0
example/order/erase_example.cpp

@@ -0,0 +1,128 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <unordered_map>
+#include <chrono>
+using namespace std;
+
+#include "tire.h"
+
+const int N = 1e5;
+const int M = 5e4;
+const int M1 = 3;
+const int M2 = 5;
+
+struct Test {
+	int a;
+	short b;
+	int c;
+};
+
+vector<long long> umap_times;
+vector<long long> tire_times;
+
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		string str = to_string(i + 1);
+		data_table.push_back(str);
+	}
+}
+
+vector<string> target_table;
+void init_target_table() {
+	for (int i = 0; i < M; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 10) + '0';
+		}
+		str[size] = '\0';
+		target_table.push_back(str);
+	}
+}
+
+void umap_test() {
+	const int n = data_table.size();
+	unordered_map<string, Test> umap;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
+	auto start = chrono::high_resolution_clock::now();
+	for (auto& target : target_table) {
+		umap.erase(target);
+	}
+	auto end = chrono::high_resolution_clock::now();
+	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
+	umap_times.push_back(duration.count());
+}
+
+void tire_test() {
+	const int n = data_table.size();
+	Tire<Test> tire;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+	auto start = chrono::high_resolution_clock::now();
+	for (auto& target : target_table) {
+		tire.erase(target);
+	}
+	auto end = chrono::high_resolution_clock::now();
+	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
+	tire_times.push_back(duration.count());
+}
+
+void analysis(string name, vector<long long>& nums) {
+	const int n = nums.size();
+	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long sum = 0;
+	for (long long& num : nums) {
+		maxn = max(maxn, num);
+		minn = min(minn, num);
+		sum += num;
+	}
+	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
+}
+
+int main() {
+	init_data_table();
+	init_target_table();
+	// group 1
+	for (int i = 0; i < M1; ++i) {
+		umap_test();
+		tire_test();
+	}
+
+	// group 2
+	for (int i = 0; i < M1; ++i) {
+		tire_test();
+		umap_test();
+	}
+
+	// group 3
+	for (int i = 0; i < M2; ++i) {
+		umap_test();
+	}
+	for (int i = 0; i < M2; ++i) {
+		tire_test();
+	}
+
+	// group 4
+	for (int i = 0; i < M2; ++i) {
+		tire_test();
+	}
+	for (int i = 0; i < M2; ++i) {
+		umap_test();
+	}
+
+	// 分析
+	analysis("umap", umap_times);
+	analysis("tire", tire_times);
+	return 0;
+}

+ 43 - 37
analysis2-2.cpp → example/order/find_example.cpp

@@ -8,23 +8,44 @@ using namespace std;
 
 #include "tire.h"
 
-const int TABLE_SIZE = 5e4;
 const int N = 1e5;
+const int M = 5e4;
 const int M1 = 3;
 const int M2 = 5;
 
 struct Test {
-	int num;
-	string str;
+	int a;
+	short b;
+	int c;
 };
 
 vector<long long> umap_times;
 vector<long long> tire_times;
 
-vector<string> table;
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		string str = to_string(i + 1);
+		data_table.push_back(str);
+	}
+}
+
+vector<string> target_table;
+void init_target_table() {
+	for (int i = 0; i < M; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 10) + '0';
+		}
+		str[size] = '\0';
+		target_table.push_back(str);
+	}
+}
+
 void umap_test(unordered_map<string, Test>& umap) {
 	auto start = chrono::high_resolution_clock::now();
-	for (auto& target : table) {
+	for (auto& target : target_table) {
 		umap.find(target);
 	}
 	auto end = chrono::high_resolution_clock::now();
@@ -34,7 +55,7 @@ void umap_test(unordered_map<string, Test>& umap) {
 
 void tire_test(Tire<Test>& tire) {
 	auto start = chrono::high_resolution_clock::now();
-	for (auto& target : table) {
+	for (auto& target : target_table) {
 		tire.find(target);
 	}
 	auto end = chrono::high_resolution_clock::now();
@@ -54,39 +75,26 @@ void analysis(string name, vector<long long>& nums) {
 	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
 }
 
-void print_times(string name, vector<long long>& nums) {
-	cout << name << endl;
-	for (long long& num : nums) {
-		cout << num << " ";
-	};
-	cout << endl;
-}
-
 int main() {
+	init_data_table();
+	init_target_table();
+
 	Tire<Test> tire;
 	unordered_map<string, Test> umap;
-	for (int i = 0; i < TABLE_SIZE; ++i) {
-		char str[32] = "";
-		int size = rand() % 30;
-		for (int j = 0; j < size; ++j) {
-			str[j] = (char)(rand() % 256);
-		}
-		str[size] = '\0';
-		table.push_back(str);
-	}
 
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		tire.insert(test.str, test);
-	};
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		umap.insert({ test.str, test });
-	};
+	const int n = data_table.size();
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
 
 	// group 1
 	for (int i = 0; i < M1; ++i) {
@@ -117,8 +125,6 @@ int main() {
 	}
 
 	// 分析
-	print_times("umap", umap_times);
-	print_times("tire", tire_times);
 	analysis("umap", umap_times);
 	analysis("tire", tire_times);
 	return 0;

+ 32 - 25
analysis1.cpp → example/order/iterator_example.cpp

@@ -3,25 +3,38 @@
 #include <vector>
 #include <unordered_map>
 #include <chrono>
+#include <random>
 using namespace std;
 
 #include "tire.h"
 
 const int N = 1e5;
+const int M = 5e4;
 const int M1 = 3;
 const int M2 = 5;
 
 struct Test {
-	int num;
-	string str;
+	int a;
+	short b;
+	int c;
 };
 
 vector<long long> umap_times;
 vector<long long> tire_times;
 
+
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		string str = to_string(i + 1);
+		data_table.push_back(str);
+	}
+}
+
 void umap_test(unordered_map<string, Test>& umap) {
 	auto start = chrono::high_resolution_clock::now();
-	for (auto& [_, item] : umap) {
+	for (auto& item : umap) {
+
 	}
 	auto end = chrono::high_resolution_clock::now();
 	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
@@ -31,6 +44,7 @@ void umap_test(unordered_map<string, Test>& umap) {
 void tire_test(Tire<Test>& tire) {
 	auto start = chrono::high_resolution_clock::now();
 	for (auto& item : tire) {
+
 	}
 	auto end = chrono::high_resolution_clock::now();
 	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
@@ -49,30 +63,25 @@ void analysis(string name, vector<long long>& nums) {
 	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
 }
 
-void print_times(string name, vector<long long>& nums) {
-	cout << name << endl;
-	for (long long& num : nums) {
-		cout << num << " ";
-	};
-	cout << endl;
-}
-
 int main() {
+	init_data_table();
+
 	Tire<Test> tire;
 	unordered_map<string, Test> umap;
 
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		tire.insert(test.str, test);
-	};
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		umap.insert({ test.str, test });
-	};
+	const int n = data_table.size();
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
 
 	// group 1
 	for (int i = 0; i < M1; ++i) {
@@ -103,8 +112,6 @@ int main() {
 	}
 
 	// 分析
-	print_times("umap", umap_times);
-	print_times("tire", tire_times);
 	analysis("umap", umap_times);
 	analysis("tire", tire_times);
 	return 0;

+ 31 - 15
analysis6.cpp → example/order/memory_1e5.cpp

@@ -6,26 +6,40 @@
 #include <cstdlib>
 using namespace std;
 
-#include "list.h"
 #include "tire.h"
 
 const int N = 1e5;
+const int M = 5e4;
 const int M1 = 3;
 const int M2 = 5;
 
 struct Test {
-	int num;
-	string str;
+	int a;
+	short b;
+	int c;
 };
 
+vector<long long> umap_times;
+vector<long long> tire_times;
+
+
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		string str = to_string(i + 1);
+		data_table.push_back(str);
+	}
+}
+
 void umap_test() {
+	const int n = data_table.size();
 	unordered_map<string, Test> umap;
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		umap.insert({ test.str, test });
-	};
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
 	for (int i = N * 2; i >= 0; i -= 17) {
 		size_t res = umap.erase(to_string(i + 1));
 	};
@@ -34,13 +48,14 @@ void umap_test() {
 }
 
 void tire_test() {
+	const int n = data_table.size();
 	Tire<Test> tire;
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		tire.insert(test.str, test);
-	};
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
 	for (int i = N * 2; i >= 0; i -= 17) {
 		size_t res = tire.erase(to_string(i + 1));
 	};
@@ -69,6 +84,7 @@ void print_times(string name, vector<long long>& nums) {
 }
 
 int main() {
+	init_data_table();
 	// group 1
 	for (int i = 0; i < M1; ++i) {
 		umap_test();

+ 33 - 37
analysis5.cpp → example/random/construct_example.cpp

@@ -5,7 +5,6 @@
 #include <chrono>
 using namespace std;
 
-#include "list.h"
 #include "tire.h"
 
 const int N = 1e5;
@@ -13,46 +12,52 @@ const int M1 = 3;
 const int M2 = 5;
 
 struct Test {
-	int num;
-	string str;
+	int a;
+	short b;
+	int c;
 };
 
 vector<long long> umap_times;
 vector<long long> tire_times;
 
-void umap_test() {
-	unordered_map<string, Test> umap;
+vector<string> data_table;
+void init_data_table() {
 	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		umap.insert({ test.str, test });
-	};
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 256);
+		}
+		str[size] = '\0';
+		data_table.push_back(str);
+	}
+}
+
+void umap_test() {
+	const int n = data_table.size();
 	auto start = chrono::high_resolution_clock::now();
-	size_t count = 0ull;
-	for (int i = N * 2; i >= 0; i -= 17) {
-		size_t res = umap.erase(to_string(i + 1));
-		count += res;
-	};
+	unordered_map<string, Test> umap;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
 	auto end = chrono::high_resolution_clock::now();
 	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
 	umap_times.push_back(duration.count());
 }
 
 void tire_test() {
-	Tire<Test> tire;
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		tire.insert(test.str, test);
-	};
+	const int n = data_table.size();
 	auto start = chrono::high_resolution_clock::now();
-	size_t count = 0ull;
-	for (int i = N * 2; i >= 0; i -= 17) {
-		size_t res = tire.erase(to_string(i + 1));
-		count += res;
-	};
+	Tire<Test> tire;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
 	auto end = chrono::high_resolution_clock::now();
 	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
 	tire_times.push_back(duration.count());
@@ -70,15 +75,8 @@ void analysis(string name, vector<long long>& nums) {
 	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
 }
 
-void print_times(string name, vector<long long>& nums) {
-	cout << name << endl;
-	for (long long& num : nums) {
-		cout << num << " ";
-	};
-	cout << endl;
-}
-
 int main() {
+	init_data_table();
 	// group 1
 	for (int i = 0; i < M1; ++i) {
 		umap_test();
@@ -108,8 +106,6 @@ int main() {
 	}
 
 	// 分析
-	print_times("umap", umap_times);
-	print_times("tire", tire_times);
 	analysis("umap", umap_times);
 	analysis("tire", tire_times);
 	return 0;

+ 120 - 0
example/random/destruct_example.cpp

@@ -0,0 +1,120 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <unordered_map>
+#include <chrono>
+using namespace std;
+
+#include "tire.h"
+
+const int N = 1e5;
+const int M1 = 3;
+const int M2 = 5;
+
+struct Test {
+	int a;
+	short b;
+	int c;
+};
+
+vector<long long> umap_times;
+vector<long long> tire_times;
+
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 256);
+		}
+		str[size] = '\0';
+		data_table.push_back(str);
+	}
+}
+
+void _umap_test() {
+	const int n = data_table.size();
+	unordered_map<string, Test> umap;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
+}
+
+void umap_test() {
+	auto start = chrono::high_resolution_clock::now();
+	_umap_test();
+	auto end = chrono::high_resolution_clock::now();
+	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
+	umap_times.push_back(duration.count());
+}
+
+void _tire_test() {
+	const int n = data_table.size();
+	Tire<Test> tire;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+}
+
+void tire_test() {
+	auto start = chrono::high_resolution_clock::now();
+	_tire_test();
+	auto end = chrono::high_resolution_clock::now();
+	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
+	tire_times.push_back(duration.count());
+}
+
+void analysis(string name, vector<long long>& nums) {
+	const int n = nums.size();
+	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long sum = 0;
+	for (long long& num : nums) {
+		maxn = max(maxn, num);
+		minn = min(minn, num);
+		sum += num;
+	}
+	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
+}
+
+int main() {
+	init_data_table();
+	// group 1
+	for (int i = 0; i < M1; ++i) {
+		umap_test();
+		tire_test();
+	}
+
+	// group 2
+	for (int i = 0; i < M1; ++i) {
+		tire_test();
+		umap_test();
+	}
+
+	// group 3
+	for (int i = 0; i < M2; ++i) {
+		umap_test();
+	}
+	for (int i = 0; i < M2; ++i) {
+		tire_test();
+	}
+
+	// group 4
+	for (int i = 0; i < M2; ++i) {
+		tire_test();
+	}
+	for (int i = 0; i < M2; ++i) {
+		umap_test();
+	}
+
+	// 分析
+	analysis("umap", umap_times);
+	analysis("tire", tire_times);
+	return 0;
+}

+ 133 - 0
example/random/erase_example.cpp

@@ -0,0 +1,133 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <unordered_map>
+#include <chrono>
+using namespace std;
+
+#include "tire.h"
+
+const int N = 1e5;
+const int M = 5e4;
+const int M1 = 3;
+const int M2 = 5;
+
+struct Test {
+	int a;
+	short b;
+	int c;
+};
+
+vector<long long> umap_times;
+vector<long long> tire_times;
+
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 256);
+		}
+		str[size] = '\0';
+		data_table.push_back(str);
+	}
+}
+
+vector<string> target_table;
+void init_target_table() {
+	for (int i = 0; i < M; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 256);
+		}
+		str[size] = '\0';
+		target_table.push_back(str);
+	}
+}
+
+void umap_test() {
+	const int n = data_table.size();
+	unordered_map<string, Test> umap;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
+	auto start = chrono::high_resolution_clock::now();
+	for (auto& target : target_table) {
+		umap.erase(target);
+	}
+	auto end = chrono::high_resolution_clock::now();
+	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
+	umap_times.push_back(duration.count());
+}
+
+void tire_test() {
+	const int n = data_table.size();
+	Tire<Test> tire;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+	auto start = chrono::high_resolution_clock::now();
+	for (auto& target : target_table) {
+		tire.erase(target);
+	}
+	auto end = chrono::high_resolution_clock::now();
+	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
+	tire_times.push_back(duration.count());
+}
+
+void analysis(string name, vector<long long>& nums) {
+	const int n = nums.size();
+	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long sum = 0;
+	for (long long& num : nums) {
+		maxn = max(maxn, num);
+		minn = min(minn, num);
+		sum += num;
+	}
+	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
+}
+
+int main() {
+	init_data_table();
+	init_target_table();
+	// group 1
+	for (int i = 0; i < M1; ++i) {
+		umap_test();
+		tire_test();
+	}
+
+	// group 2
+	for (int i = 0; i < M1; ++i) {
+		tire_test();
+		umap_test();
+	}
+
+	// group 3
+	for (int i = 0; i < M2; ++i) {
+		umap_test();
+	}
+	for (int i = 0; i < M2; ++i) {
+		tire_test();
+	}
+
+	// group 4
+	for (int i = 0; i < M2; ++i) {
+		tire_test();
+	}
+	for (int i = 0; i < M2; ++i) {
+		umap_test();
+	}
+
+	// 分析
+	analysis("umap", umap_times);
+	analysis("tire", tire_times);
+	return 0;
+}

+ 136 - 0
example/random/find_example.cpp

@@ -0,0 +1,136 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <unordered_map>
+#include <chrono>
+#include <random>
+using namespace std;
+
+#include "tire.h"
+
+const int N = 1e5;
+const int M = 5e4;
+const int M1 = 3;
+const int M2 = 5;
+
+struct Test {
+	int a;
+	short b;
+	int c;
+};
+
+vector<long long> umap_times;
+vector<long long> tire_times;
+
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 256);
+		}
+		str[size] = '\0';
+		data_table.push_back(str);
+	}
+}
+
+vector<string> target_table;
+void init_target_table() {
+	for (int i = 0; i < M; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 256);
+		}
+		str[size] = '\0';
+		target_table.push_back(str);
+	}
+}
+
+void umap_test(unordered_map<string, Test>& umap) {
+	auto start = chrono::high_resolution_clock::now();
+	for (auto& target : target_table) {
+		umap.find(target);
+	}
+	auto end = chrono::high_resolution_clock::now();
+	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
+	umap_times.push_back(duration.count());
+}
+
+void tire_test(Tire<Test>& tire) {
+	auto start = chrono::high_resolution_clock::now();
+	for (auto& target : target_table) {
+		tire.find(target);
+	}
+	auto end = chrono::high_resolution_clock::now();
+	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
+	tire_times.push_back(duration.count());
+}
+
+void analysis(string name, vector<long long>& nums) {
+	const int n = nums.size();
+	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long sum = 0;
+	for (long long& num : nums) {
+		maxn = max(maxn, num);
+		minn = min(minn, num);
+		sum += num;
+	}
+	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
+}
+
+int main() {
+	init_data_table();
+	init_target_table();
+
+	Tire<Test> tire;
+	unordered_map<string, Test> umap;
+
+	const int n = data_table.size();
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
+
+	// group 1
+	for (int i = 0; i < M1; ++i) {
+		umap_test(umap);
+		tire_test(tire);
+	}
+
+	// group 2
+	for (int i = 0; i < M1; ++i) {
+		tire_test(tire);
+		umap_test(umap);
+	}
+
+	// group 3
+	for (int i = 0; i < M2; ++i) {
+		umap_test(umap);
+	}
+	for (int i = 0; i < M2; ++i) {
+		tire_test(tire);
+	}
+
+	// group 4
+	for (int i = 0; i < M2; ++i) {
+		tire_test(tire);
+	}
+	for (int i = 0; i < M2; ++i) {
+		umap_test(umap);
+	}
+
+	// 分析
+	analysis("umap", umap_times);
+	analysis("tire", tire_times);
+	return 0;
+}

+ 37 - 33
analysis2.cpp → example/random/iterator_example.cpp

@@ -3,29 +3,42 @@
 #include <vector>
 #include <unordered_map>
 #include <chrono>
+#include <random>
 using namespace std;
 
 #include "tire.h"
 
 const int N = 1e5;
+const int M = 5e4;
 const int M1 = 3;
 const int M2 = 5;
 
 struct Test {
-	int num;
-	string str;
+	int a;
+	short b;
+	int c;
 };
 
 vector<long long> umap_times;
 vector<long long> tire_times;
 
-const string table[16] = { "12345", "99999", "97282", "1", "2","3","7", "2222", "22", "3422", "7482", "9374", "000" };
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 256);
+		}
+		str[size] = '\0';
+		data_table.push_back(str);
+	}
+}
+
 void umap_test(unordered_map<string, Test>& umap) {
 	auto start = chrono::high_resolution_clock::now();
-	for (int j = 0; j < 1e4; ++j) {
-		for (int i = 0; i < 16; ++i) {
-			umap.find(table[i]);
-		}
+	for (auto& item : umap) {
+
 	}
 	auto end = chrono::high_resolution_clock::now();
 	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
@@ -34,10 +47,8 @@ void umap_test(unordered_map<string, Test>& umap) {
 
 void tire_test(Tire<Test>& tire) {
 	auto start = chrono::high_resolution_clock::now();
-	for (int j = 0; j < 1e4; ++j) {
-		for (int i = 0; i < 16; ++i) {
-			tire.find(table[i]);
-		}
+	for (auto& item : tire) {
+
 	}
 	auto end = chrono::high_resolution_clock::now();
 	auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
@@ -56,30 +67,25 @@ void analysis(string name, vector<long long>& nums) {
 	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
 }
 
-void print_times(string name, vector<long long>& nums) {
-	cout << name << endl;
-	for (long long& num : nums) {
-		cout << num << " ";
-	};
-	cout << endl;
-}
-
 int main() {
+	init_data_table();
+
 	Tire<Test> tire;
 	unordered_map<string, Test> umap;
 
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		tire.insert(test.str, test);
-	};
-	for (int i = 0; i < N; ++i) {
-		Test test;
-		test.num = i + 1;
-		test.str = to_string(i + 1);
-		umap.insert({ test.str, test });
-	};
+	const int n = data_table.size();
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
 
 	// group 1
 	for (int i = 0; i < M1; ++i) {
@@ -110,8 +116,6 @@ int main() {
 	}
 
 	// 分析
-	print_times("umap", umap_times);
-	print_times("tire", tire_times);
 	analysis("umap", umap_times);
 	analysis("tire", tire_times);
 	return 0;

+ 104 - 0
example/random/memory_1e5.cpp

@@ -0,0 +1,104 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <unordered_map>
+#include <chrono>
+#include <cstdlib>
+using namespace std;
+
+#include "tire.h"
+
+const int N = 1e5;
+const int M = 5e4;
+const int M1 = 3;
+const int M2 = 5;
+
+struct Test {
+	int a;
+	short b;
+	int c;
+};
+
+vector<long long> umap_times;
+vector<long long> tire_times;
+
+vector<string> data_table;
+void init_data_table() {
+	for (int i = 0; i < N; ++i) {
+		char str[32] = "";
+		int size = rand() % 30;
+		for (int j = 0; j < size; ++j) {
+			str[j] = (char)(rand() % 256);
+		}
+		str[size] = '\0';
+		data_table.push_back(str);
+	}
+}
+
+void umap_test() {
+	const int n = data_table.size();
+	unordered_map<string, Test> umap;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		umap.insert({ data_table[i], t });
+	}
+	for (int i = N * 2; i >= 0; i -= 17) {
+		size_t res = umap.erase(to_string(i + 1));
+	};
+	cout << "umap ";
+	system("pause");
+}
+
+void tire_test() {
+	const int n = data_table.size();
+	Tire<Test> tire;
+	for (int i = 0; i < n; ++i) {
+		Test t;
+		t.a = i;
+		t.c = n + i;
+		tire.insert(data_table[i], t);
+	}
+	for (int i = N * 2; i >= 0; i -= 17) {
+		size_t res = tire.erase(to_string(i + 1));
+	};
+	cout << "tire ";
+	system("pause");
+}
+
+void analysis(string name, vector<long long>& nums) {
+	const int n = nums.size();
+	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long sum = 0;
+	for (long long& num : nums) {
+		maxn = max(maxn, num);
+		minn = min(minn, num);
+		sum += num;
+	}
+	cout << name << ": " << "maxn: " << maxn << "ms    " << "minn: " << minn << "ms    " << "avg: " << (1.0 * sum / n) << "ms    " << "sum: " << sum << "ms    " << endl;
+}
+
+void print_times(string name, vector<long long>& nums) {
+	cout << name << endl;
+	for (long long& num : nums) {
+		cout << num << " ";
+	};
+	cout << endl;
+}
+
+int main() {
+	init_data_table();
+	// group 1
+	for (int i = 0; i < M1; ++i) {
+		umap_test();
+		tire_test();
+	}
+
+	// group 2
+	for (int i = 0; i < M1; ++i) {
+		tire_test();
+		umap_test();
+	}
+	return 0;
+}