Browse Source

添加 Ubuntu 随机生成查找目标测试

ToiletMaster 10 months ago
parent
commit
618949a800
8 changed files with 165 additions and 46 deletions
  1. 1 1
      analysis1.cpp
  2. 125 0
      analysis2-2.cpp
  3. 1 1
      analysis2.cpp
  4. 1 1
      analysis3.cpp
  5. 1 1
      analysis4.cpp
  6. 1 1
      analysis5.cpp
  7. 35 39
      analysis6.cpp
  8. 0 2
      list.h

+ 1 - 1
analysis1.cpp

@@ -39,7 +39,7 @@ void tire_test(Tire<Test>& tire) {
 
 void analysis(string name, vector<long long>& nums) {
 	const int n = nums.size();
-	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long maxn = 0, minn = 0x7fffffffffffffff;
 	long long sum = 0;
 	for (long long& num : nums) {
 		maxn = max(maxn, num);

+ 125 - 0
analysis2-2.cpp

@@ -0,0 +1,125 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <unordered_map>
+#include <chrono>
+#include <random>
+using namespace std;
+
+#include "tire.h"
+
+const int TABLE_SIZE = 5e4;
+const int N = 1e5;
+const int M1 = 3;
+const int M2 = 5;
+
+struct Test {
+	int num;
+	string str;
+};
+
+vector<long long> umap_times;
+vector<long long> tire_times;
+
+vector<string> table;
+void umap_test(unordered_map<string, Test>& umap) {
+	auto start = chrono::high_resolution_clock::now();
+	for (auto& 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 : 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 = 0, minn = 0x7fffffffffffffff;
+	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() {
+	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 });
+	};
+
+	// 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);
+	}
+
+	// 分析
+	print_times("umap", umap_times);
+	print_times("tire", tire_times);
+	analysis("umap", umap_times);
+	analysis("tire", tire_times);
+	return 0;
+}

+ 1 - 1
analysis2.cpp

@@ -46,7 +46,7 @@ void tire_test(Tire<Test>& tire) {
 
 void analysis(string name, vector<long long>& nums) {
 	const int n = nums.size();
-	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long maxn = 0, minn = 0x7fffffffffffffff;
 	long long sum = 0;
 	for (long long& num : nums) {
 		maxn = max(maxn, num);

+ 1 - 1
analysis3.cpp

@@ -49,7 +49,7 @@ void tire_test() {
 
 void analysis(string name, vector<long long>& nums) {
 	const int n = nums.size();
-	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long maxn = 0, minn = 0x7fffffffffffffff;
 	long long sum = 0;
 	for (long long& num : nums) {
 		maxn = max(maxn, num);

+ 1 - 1
analysis4.cpp

@@ -57,7 +57,7 @@ void tire_test() {
 
 void analysis(string name, vector<long long>& nums) {
 	const int n = nums.size();
-	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long maxn = 0, minn = 0x7fffffffffffffff;
 	long long sum = 0;
 	for (long long& num : nums) {
 		maxn = max(maxn, num);

+ 1 - 1
analysis5.cpp

@@ -60,7 +60,7 @@ void tire_test() {
 
 void analysis(string name, vector<long long>& nums) {
 	const int n = nums.size();
-	long long maxn = LLONG_MIN, minn = LLONG_MAX;
+	long long maxn = 0, minn = 0x7fffffffffffffff;
 	long long sum = 0;
 	for (long long& num : nums) {
 		maxn = max(maxn, num);

+ 35 - 39
analysis6.cpp

@@ -4,8 +4,28 @@
 #include <unordered_map>
 #include <chrono>
 #include <cstdlib>
+#include <fstream>
 using namespace std;
 
+#include <unistd.h>
+
+int GetSysMemInfo() {  //获取系统当前可用内存
+	int tSize = 0, resident = 0, share = 0;
+	ifstream buffer("/proc/self/statm");
+	buffer >> tSize >> resident >> share;
+	buffer.close();
+
+	long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
+	double rss = resident * page_size_kb;
+	cout << "RSS: " << rss << " kB.\t\t";
+
+	double shared_mem = share * page_size_kb;
+	cout << "Shared Memory: " << shared_mem << " kB.\t\t";
+
+	cout << "Private Memory: " << rss - shared_mem << "kB." << endl;
+	return 0;
+}
+
 #include "list.h"
 #include "tire.h"
 
@@ -18,6 +38,7 @@ struct Test {
 	string str;
 };
 
+
 void umap_test() {
 	unordered_map<string, Test> umap;
 	for (int i = 0; i < N; ++i) {
@@ -26,11 +47,8 @@ void umap_test() {
 		test.str = to_string(i + 1);
 		umap.insert({ test.str, test });
 	};
-	for (int i = N * 2; i >= 0; i -= 17) {
-		size_t res = umap.erase(to_string(i + 1));
-	};
-	cout << "umap ";
-	system("pause");
+	cout << "umap: ";
+	GetSysMemInfo();
 }
 
 void tire_test() {
@@ -41,44 +59,22 @@ void tire_test() {
 		test.str = to_string(i + 1);
 		tire.insert(test.str, test);
 	};
-	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;
+	cout << "tire: ";
+	GetSysMemInfo();
 }
 
 int main() {
-	// group 1
-	for (int i = 0; i < M1; ++i) {
-		umap_test();
-		tire_test();
-	}
+	Tire<Test> tire;
+	unordered_map<string, Test> umap;
+	cout << "main: ";
+	GetSysMemInfo();
 
 	// group 2
-	for (int i = 0; i < M1; ++i) {
-		tire_test();
-		umap_test();
-	}
+	umap_test();
+	// group 1
+	tire_test();
+
+	cout << "main: ";
+	GetSysMemInfo();
 	return 0;
 }

+ 0 - 2
list.h

@@ -1,8 +1,6 @@
 #ifndef __L87_LIST_H__
 #define __L87_LIST_H__
 
-#include <crtdefs.h>
-
 template<typename Type>
 class List {
 public: