Browse Source

algorithm test

Hongtae Kim 6 years ago
parent
commit
6388192ec9
1 changed files with 69 additions and 82 deletions
  1. 69
    82
      TestApp1/TestApp1.cpp

+ 69
- 82
TestApp1/TestApp1.cpp View File

@@ -5,100 +5,87 @@
5 5
 #include "Win32/stdafx.h"
6 6
 #endif
7 7
 
8
+#include <algorithm>
8 9
 #include <DK.h>
9 10
 
10
-auto FormatBytes(size_t size, int maxDigits=3)->DKString
11
+#define MAX_SIZE 100000000ULL
12
+
13
+#ifdef _WIN32
14
+int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
15
+	_In_opt_ HINSTANCE hPrevInstance,
16
+	_In_ LPWSTR    lpCmdLine,
17
+	_In_ int       nCmdShow)
18
+#else
19
+int main(int argc, const char * argv[])
20
+#endif
11 21
 {
12
-	if (size > 1)
22
+	union Value
13 23
 	{
14
-		maxDigits = Clamp(maxDigits, 1, 4);
15
-
16
-		const char* suffixList[7] = { "Bytes","KB","MB","GB","TB","PB","EB" };
17
-		int suffixIndex = 0;
18
-		while (size >= 1024000 && suffixIndex < 6)
19
-		{
20
-			suffixIndex++;
21
-			size = size >> 10;
22
-		}
23
-		double s = static_cast<double>(size);
24
-		if (size >= 1000 && suffixIndex < 6)
25
-		{
26
-			s = s / 1024.0;
27
-			suffixIndex++;
28
-		}
29
-		if (suffixIndex > 0)
24
+		size_t i64;
25
+		struct
30 26
 		{
31
-			int numDigits = [](int s)->int
32
-			{
33
-				int n = 0;
34
-				while (s > 0)
35
-				{
36
-					n++;
37
-					s /= 10;
38
-				}
39
-				return Max(n, 1);
40
-			}(s);
41
-			return DKString::Format("%.*f %s", Clamp<int>(maxDigits - numDigits, 0, 3), s, suffixList[suffixIndex]);
42
-		}
43
-		return DKString::Format("%d %s", int(size), suffixList[0]);
44
-	}
45
-	return DKString::Format("%d Byte", int(size));
46
-};
27
+			uint32_t h, l;
28
+		} i32;
29
+	};
47 30
 
48
-class TestApp1 : public DKApplication
49
-{
50
-	DKObject<DKWindow> window;
51
-public:
52
-	void OnInitialize(void) override
31
+
32
+
33
+	auto testStd = [](Value* val_copy, const Value* input, size_t len)->DKHashResultSHA1
53 34
 	{
54
-		DKLogD("%s", DKGL_FUNCTION_NAME);
55
-		window = DKWindow::Create("DefaultWindow");
56
-		window->Activate();
35
+		DKLogI("Copying buffer...");
36
+		memcpy(val_copy, input, sizeof(Value) * len);
57 37
 
58
-		window->AddEventHandler(this,
59
-			DKFunction([this](const DKWindow::WindowEvent& e) {
60
-			if (e.type == DKWindow::WindowEvent::WindowClosed)
61
-				DKApplication::Instance()->Terminate(0);
62
-		}),
63
-			NULL, NULL);
38
+		DKTimer timer;
39
+		DKLogI("Sorting by std::sort...");
40
+		timer.Reset();
41
+		std::sort(val_copy, val_copy + MAX_SIZE, [](const Value& lhs, const Value& rhs)->bool
42
+		{
43
+			return lhs.i64 < rhs.i64;
44
+		});
45
+		double e1 = timer.Elapsed();
64 46
 
65
-	}
66
-	void OnTerminate(void) override
47
+		DKLogI("std::sort: %f", e1);
48
+		return DKHashSHA1(val_copy, sizeof(Value)*len);
49
+	};
50
+	auto testDK = [](Value* val_copy, const Value* input, size_t len)->DKHashResultSHA1
67 51
 	{
68
-		DKLogD("%s", DKGL_FUNCTION_NAME);
69
-		DKLogI("Memory Pool Statistics");
70
-		size_t numBuckets = DKMemoryPoolNumberOfBuckets();
71
-		DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
72
-		DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
73
-		size_t usedBytes = 0;
74
-		for (int i = 0; i < numBuckets; ++i)
52
+		DKLogI("Copying buffer...");
53
+		memcpy(val_copy, input, sizeof(Value) * len);
54
+
55
+		DKStaticArray<Value> tmp(val_copy, MAX_SIZE);
56
+
57
+		DKTimer timer;
58
+		DKLogI("Sorting by DKStaticArray::Sort...");
59
+		timer.Reset();
60
+		tmp.Sort([](const Value& lhs, const Value& rhs)->bool
75 61
 		{
76
-			if (buckets[i].totalChunks > 0)
77
-			{
78
-				DKLogI("--> %lu: allocated:%lu, reserved:%ls. (usage:%.1f%%)",
79
-					buckets[i].chunkSize,
80
-					buckets[i].chunkSize * buckets[i].usedChunks,
81
-					(const wchar_t*)FormatBytes(buckets[i].chunkSize * (buckets[i].totalChunks - buckets[i].usedChunks)),
82
-					double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0);
83
-				usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
84
-			}
85
-		}
86
-		DKLogI("MemoryPool Usage: %ls / %ls", (const wchar_t*)FormatBytes(usedBytes), (const wchar_t*)FormatBytes(DKMemoryPoolSize()));
87
-		delete[] buckets;
62
+			return lhs.i64 < rhs.i64;
63
+		});
64
+		double e1 = timer.Elapsed();
65
+
66
+		DKLogI("DKStaticArray::sort: %f (memCopy:%d)", e1, tmp.UseMemoryCopy);
67
+		return DKHashSHA1(val_copy, sizeof(Value)*len);
68
+	};
69
+
70
+	DKApplication app;
71
+	DKLog("Allocating buffer...");
72
+
73
+	Value* val1 = new Value[MAX_SIZE];
74
+
75
+	for (size_t i = 0; i < MAX_SIZE; ++i)
76
+	{
77
+		val1[i].i32 = { DKRandom(), DKRandom() };
88 78
 	}
89
-};
79
+	Value* val_copy = new Value[MAX_SIZE];
90 80
 
81
+	DKHashResultSHA1 sha1_r1 = testDK(val_copy, val1, MAX_SIZE);
82
+	DKLogI("Hash1: %ls", (const wchar_t*)sha1_r1.String());
83
+	DKHashResultSHA1 sha1_r2 = testStd(val_copy, val1, MAX_SIZE);
84
+	DKLogI("Hash2: %ls", (const wchar_t*)sha1_r2.String());
85
+	DKLogI("Hash compare:%d", sha1_r1.Compare(sha1_r2));
91 86
 
92
-#ifdef _WIN32
93
-int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
94
-	_In_opt_ HINSTANCE hPrevInstance,
95
-	_In_ LPWSTR    lpCmdLine,
96
-	_In_ int       nCmdShow)
97
-#else
98
-int main(int argc, const char * argv[])
99
-#endif
100
-{
101
-	TestApp1 app;
102
-	DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
103
-	return app.Run();
87
+	delete[] val1;
88
+	delete[] val_copy;
89
+
90
+	return 0;
104 91
 }