No Description

TestApp1.cpp 3.5KB

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