123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // TestApp1.cpp : Defines the entry point for the application.
- //
-
- #ifdef _WIN32
- #include "Win32/stdafx.h"
- #endif
-
- #include <algorithm>
- #include <DK.h>
-
-
- #define MAX_SIZE 80000000ULL
-
- struct Value64
- {
- int num;
- int dummy[15];
- bool operator < (const Value64& r) const
- {
- return this->num < r.num;
- }
- };
-
- struct Value16
- {
- int num;
- int dummy[3];
- bool operator < (const Value16& r) const
- {
- return this->num < r.num;
- }
- };
-
- union Value8
- {
- size_t i64;
- struct
- {
- uint32_t h, l;
- } i32;
- bool operator < (const Value8& r) const
- {
- return this->i64 < r.i64;
- }
- };
-
- using Value4 = uint32_t;
-
-
- template <typename T, typename Rand>
- void DoTest(bool dkFirst, Rand&& r)
- {
- T* val1 = new T[MAX_SIZE];
- T* val2 = new T[MAX_SIZE];
-
- DKLog("Generating data... (%s)", typeid(T).name());
- for (size_t i = 0; i < MAX_SIZE; ++i)
- val1[i] = val2[i] = r();
-
-
- DKThread::Sleep(0.2);
-
- using ResultTuple = DKTuple<DKHashResultSHA1, double>;
-
- auto testSTL = [](T* val, size_t len)->ResultTuple
- {
- DKLog("Sorting by std::sort...");
- DKTimer timer;
- timer.Reset();
- std::sort(val, val + len, [](const auto& lhs, const auto& rhs)->bool
- {
- return lhs < rhs;
- });
- double e1 = timer.Elapsed();
- DKLogW("std::sort: %f", e1);
- auto hash = DKHashSHA1(val, sizeof(T) * len);
- DKLog("std::sort hash: %ls", (const wchar_t*)hash.String());
- return ResultTuple::Make(hash, e1);
- };
-
- auto testDK = [](T* val, size_t len)->ResultTuple
- {
- DKLog("Sorting by DKStaticArray::Sort...");
- DKStaticArray<T> tmp(val, len);
- DKTimer timer;
- timer.Reset();
- tmp.Sort([](const auto& lhs, const auto& rhs)->bool
- {
- return lhs < rhs;
- });
- double e1 = timer.Elapsed();
- DKLogW("DKStaticArray::Sort: %f (SwapMethod:%d)", e1, tmp.UseMemoryCopy);
- auto hash = DKHashSHA1(val, sizeof(T) * len);
- DKLog("DKStaticArray::Sort hash: %ls", (const wchar_t*)hash.String());
- return ResultTuple::Make(hash, e1);
- };
-
- if (dkFirst)
- {
- auto result1 = testDK(val1, MAX_SIZE);
- auto result2 = testSTL(val2, MAX_SIZE);
- if (result1.Value<0>().Compare(result2.Value<0>()))
- DKLogE("Hash Compare: %d (ERROR)", result1.Value<0>().Compare(result2.Value<0>()));
- else
- DKLogI("Hash compare: %d (time: %f)",
- result1.Value<0>().Compare(result2.Value<0>()),
- result1.Value<1>() - result2.Value<1>());
- }
- else
- {
- auto result1 = testSTL(val1, MAX_SIZE);
- auto result2 = testDK(val2, MAX_SIZE);
- if (result1.Value<0>().Compare(result2.Value<0>()))
- DKLogE("Hash Compare: %d (ERROR)", result1.Value<0>().Compare(result2.Value<0>()));
- else
- DKLogI("Hash compare: %d (time: %f)",
- result1.Value<0>().Compare(result2.Value<0>()),
- result1.Value<1>() - result2.Value<1>());
- }
-
- delete[] val1;
- delete[] val2;
- DKLog("--------------");
- DKThread::Sleep(0.2);
- }
-
- #ifdef _WIN32
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- #else
- int main(int argc, const char * argv[])
- #endif
- {
- DKApplication app;
-
- bool dkFirst = true;
-
- DoTest<Value64>(dkFirst, []()->Value64
- {
- Value64 val = { 0 };
- val.num = DKRandom();
- return val;
- });
- DoTest<Value16>(dkFirst, []()->Value16
- {
- Value16 val = { 0 };
- val.num = DKRandom();
- return val;
- });
- DoTest<Value8>(dkFirst, []()->Value8
- {
- return { size_t(DKRandom()) | (size_t(DKRandom()) << 32) };
- });
- DoTest<Value4>(dkFirst, []()->Value4
- {
- return DKRandom();
- });
-
- return 0;
- }
|