// TestApp1.cpp : Defines the entry point for the application. // #ifdef _WIN32 #include "Win32/stdafx.h" #endif #include #include #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 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; 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 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.template Value<0>().Compare(result2.template Value<0>())) DKLogE("Hash Compare: %d (ERROR)", result1.template Value<0>().Compare(result2.template Value<0>())); else DKLogI("Hash compare: %d (time: %f)", result1.template Value<0>().Compare(result2.template Value<0>()), result1.template Value<1>() - result2.template Value<1>()); } else { auto result1 = testSTL(val1, MAX_SIZE); auto result2 = testDK(val2, MAX_SIZE); if (result1.template Value<0>().Compare(result2.template Value<0>())) DKLogE("Hash Compare: %d (ERROR)", result1.template Value<0>().Compare(result2.template Value<0>())); else DKLogI("Hash compare: %d (time: %f)", result1.template Value<0>().Compare(result2.template Value<0>()), result1.template Value<1>() - result2.template 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(dkFirst, []()->Value64 { Value64 val = { 0 }; val.num = DKRandom(); return val; }); DoTest(dkFirst, []()->Value16 { Value16 val = { 0 }; val.num = DKRandom(); return val; }); DoTest(dkFirst, []()->Value8 { return { size_t(DKRandom()) | (size_t(DKRandom()) << 32) }; }); DoTest(dkFirst, []()->Value4 { return DKRandom(); }); return 0; }