Hongtae Kim hace 6 años
padre
commit
ba18d45877
Se han modificado 1 ficheros con 108 adiciones y 66 borrados
  1. 108
    66
      TestApp1/TestApp1.cpp

+ 108
- 66
TestApp1/TestApp1.cpp Ver fichero

@@ -9,98 +9,140 @@
9 9
 #include <DK.h>
10 10
 
11 11
 
12
-#define MAX_SIZE 200000000ULL
12
+#define MAX_SIZE 50000000ULL
13 13
 
14
-#ifdef _WIN32
15
-int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
16
-	_In_opt_ HINSTANCE hPrevInstance,
17
-	_In_ LPWSTR    lpCmdLine,
18
-	_In_ int       nCmdShow)
19
-#else
20
-int main(int argc, const char * argv[])
21
-#endif
14
+struct Value64
22 15
 {
23
-	union Value
16
+	int num;
17
+	int dummy[15];
18
+	bool operator < (const Value64& r) const
24 19
 	{
25
-		size_t i64;
26
-		struct
27
-		{
28
-			uint32_t h, l;
29
-		} i32;
30
-	};
20
+		return this->num < r.num;
21
+	}
22
+};
31 23
 
32
-	auto testStd = [](Value* val_copy, const Value* input, size_t len)->DKHashResultSHA1
24
+struct Value16
25
+{
26
+	int num;
27
+	int dummy[3];
28
+	bool operator < (const Value16& r) const
33 29
 	{
34
-		DKLogI("Copying buffer...");
35
-		memcpy(val_copy, input, sizeof(Value) * len);
30
+		return this->num < r.num;
31
+	}
32
+};
36 33
 
37
-		DKTimer timer;
38
-		DKThread::Sleep(1.0);
34
+union Value8
35
+{
36
+	size_t i64;
37
+	struct
38
+	{
39
+		uint32_t h, l;
40
+	} i32;
41
+	bool operator < (const Value8& r) const
42
+	{
43
+		return this->i64 < r.i64;
44
+	}
45
+};
46
+
47
+using Value4 = uint32_t;
48
+
49
+
50
+template <typename T, typename Rand>
51
+void DoTest(bool dkFirst, Rand&& r)
52
+{
53
+	T* val1 = new T[MAX_SIZE];
54
+	T* val2 = new T[MAX_SIZE];
55
+
56
+	DKLog("Generating data... (%s)", typeid(T).name());
57
+	for (size_t i = 0; i < MAX_SIZE; ++i)
58
+		val1[i] = val2[i] = r();
59
+
60
+	
61
+	DKThread::Sleep(0.2);
62
+
63
+	auto testSTL = [](T* val, size_t len)->DKHashResultSHA1
64
+	{
39 65
 		DKLogI("Sorting by std::sort...");
66
+		DKTimer timer;
40 67
 		timer.Reset();
41
-		std::sort(val_copy, val_copy + MAX_SIZE, [](const Value& lhs, const Value& rhs)->bool
68
+		std::sort(val, val + len, [](const auto& lhs, const auto& rhs)->bool
42 69
 		{
43
-			return lhs.i64 < rhs.i64;
70
+			return lhs < rhs;
44 71
 		});
45 72
 		double e1 = timer.Elapsed();
46
-
47 73
 		DKLogW("std::sort: %f", e1);
48
-		DKThread::Sleep(1.0);
49
-
50
-		return DKHashSHA1(val_copy, sizeof(Value)*len);
74
+		auto hash = DKHashSHA1(val, sizeof(T) * len);
75
+		DKLog("std::sort hash: %ls", (const wchar_t*)hash.String());
76
+		return hash;
51 77
 	};
52
-	auto testDK = [](Value* val_copy, const Value* input, size_t len)->DKHashResultSHA1
53
-	{
54
-		DKLogI("Copying buffer...");
55
-		memcpy(val_copy, input, sizeof(Value) * len);
56 78
 
57
-		DKStaticArray<Value> tmp(val_copy, MAX_SIZE);
58
-
59
-		DKTimer timer;
60
-		DKThread::Sleep(1.0);
79
+	auto testDK = [](T* val, size_t len)->DKHashResultSHA1
80
+	{
61 81
 		DKLogI("Sorting by DKStaticArray::Sort...");
82
+		DKStaticArray<T> tmp(val, len);
83
+		DKTimer timer;
62 84
 		timer.Reset();
63
-		tmp.Sort([](const Value& lhs, const Value& rhs)->bool
85
+		tmp.Sort([](const auto& lhs, const auto& rhs)->bool
64 86
 		{
65
-			return lhs.i64 < rhs.i64;
87
+			return lhs < rhs;
66 88
 		});
67
-		double e1 = timer.Elapsed();
68
-
69
-		DKLogW("DKStaticArray::sort: %f (memCopy:%d)", e1, tmp.SwapMethod);
70
-		DKThread::Sleep(1.0);
71
-
72
-		return DKHashSHA1(val_copy, sizeof(Value)*len);
89
+		double e2 = timer.Elapsed();
90
+		DKLogW("DKStaticArray::Sort: %f (SwapMethod:%d)", e2, tmp.SwapMethod);
91
+		auto hash = DKHashSHA1(val, sizeof(T) * len);
92
+		DKLog("DKStaticArray::Sort hash: %ls", (const wchar_t*)hash.String());
93
+		return hash;
73 94
 	};
74 95
 
75
-	DKApplication app;
76
-	Value* val1 = new Value[MAX_SIZE];
77
-
78
-	DKLog("Generating data...1");
79
-	for (size_t i = 0; i < MAX_SIZE; ++i)
96
+	if (dkFirst)
80 97
 	{
81
-		val1[i].i32 = { DKRandom(), DKRandom() };
98
+		auto hash1 = testDK(val1, MAX_SIZE);
99
+		auto hash2 = testSTL(val2, MAX_SIZE);
100
+		DKLog("Hash compare: %d", hash1.Compare(hash2));
82 101
 	}
83
-	Value* val_copy = new Value[MAX_SIZE];
84
-
85
-	auto doTest = [&]() {
86
-		DKHashResultSHA1 sha1_r1 = testDK(val_copy, val1, MAX_SIZE);
87
-		DKLogI("Hash1: %ls", (const wchar_t*)sha1_r1.String());
88
-		DKHashResultSHA1 sha1_r2 = testStd(val_copy, val1, MAX_SIZE);
89
-		DKLogI("Hash2: %ls", (const wchar_t*)sha1_r2.String());
90
-		DKLogI("Hash compare:%d", sha1_r1.Compare(sha1_r2));
91
-	};
92
-	doTest();
93
-
94
-	DKLog("Generating data...2");
95
-	for (size_t i = 0; i < MAX_SIZE; ++i)
102
+	else
96 103
 	{
97
-		val1[i].i32 = { DKRandom(), DKRandom() };
104
+		auto hash1 = testSTL(val1, MAX_SIZE);
105
+		auto hash2 = testDK(val2, MAX_SIZE);
106
+		DKLog("Hash compare: %d", hash1.Compare(hash2));
98 107
 	}
99 108
 
100
-	doTest();
101
-
102 109
 	delete[] val1;
103
-	delete[] val_copy;
110
+	delete[] val2;
111
+	DKLog("--------------");
112
+	DKThread::Sleep(0.2);
113
+}
114
+
115
+#ifdef _WIN32
116
+int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
117
+	_In_opt_ HINSTANCE hPrevInstance,
118
+	_In_ LPWSTR    lpCmdLine,
119
+	_In_ int       nCmdShow)
120
+#else
121
+int main(int argc, const char * argv[])
122
+#endif
123
+{
124
+	DKApplication app;
125
+
126
+	DoTest<Value64>(true, []()->Value64
127
+	{
128
+		Value64 val;
129
+		val.num = DKRandom();
130
+		return val;
131
+	});
132
+	DoTest<Value16>(true, []()->Value16
133
+	{
134
+		Value16 val;
135
+		val.num = DKRandom();
136
+		return val;
137
+	});
138
+	DoTest<Value8>(true, []()->Value8
139
+	{
140
+		return { size_t(DKRandom()) | (size_t(DKRandom()) << 32) };
141
+	});
142
+	DoTest<Value4>(true, []()->Value4
143
+	{
144
+		return DKRandom();
145
+	});
104 146
 
105 147
 	return 0;
106 148
 }