Advertisement
Guest User

Untitled

a guest
Dec 19th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. struct X
  2. {
  3.     int value;
  4.     bool included;
  5. };
  6.  
  7. struct X2
  8. {
  9.     int value;
  10.     bool included;
  11.     X2* next;
  12. };
  13.  
  14. static const auto v = sys::Random::Value<int>();
  15.  
  16. __declspec(noinline) void consumeValue(int x)
  17. {
  18.     if (!v) [[unlikely]]
  19.         core::Log::OutInfoFmt("{}", x);
  20. }
  21.  
  22. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  23. {
  24.     constexpr auto NUM = 200;
  25.     constexpr auto ITERATIONS = 5000;
  26.     constexpr auto SEED = 10000;
  27.     constexpr auto CHANCE = 0.001f;
  28.  
  29.     const auto generateValue = [&]
  30.     {
  31.         return sys::Random::Max<int>(100000);
  32.     };
  33.  
  34.     // vector
  35.     {
  36.         core::Timer timer;
  37.  
  38.         sys::Vector<X, sys::CompactSizeT> vTest(NUM, sys::ReserveHint);
  39.  
  40.         sys::Random::InitSeed(SEED);
  41.  
  42.         for (const auto i : sys::iterateTo(NUM))
  43.         {
  44.             vTest.EmplaceBackReserved(generateValue(), sys::Random::Chance(CHANCE));
  45.         }
  46.  
  47.         core::Log::OutInfoFmt("Vector-creation took {}s", timer.Duration());
  48.  
  49.         {
  50.             timer.Reset();
  51.             for (const auto i : sys::iterateTo(ITERATIONS))
  52.             {
  53.                 for (auto& in : vTest)
  54.                 {
  55.                     if (in.included)
  56.                     {
  57.                         consumeValue(in.value);
  58.                         in.included = false;
  59.                         break;
  60.                     }
  61.                 }
  62.             }
  63.  
  64.             const auto duration = timer.Duration();
  65.             core::Log::OutInfoFmt("Vector-search took {}s", duration);
  66.         }
  67.     }
  68.  
  69.     // list
  70.     {
  71.         core::Timer timer;
  72.  
  73.         std::list<X> vTest;
  74.  
  75.         sys::Random::InitSeed(SEED);
  76.  
  77.         for (const auto i : sys::iterateTo(NUM))
  78.         {
  79.             vTest.emplace_back(generateValue(), sys::Random::Chance(CHANCE));
  80.         }
  81.  
  82.         core::Log::OutInfoFmt("List-creation took {}s", timer.Duration());
  83.  
  84.         {
  85.             timer.Reset();
  86.             for (const auto i : sys::iterateTo(ITERATIONS))
  87.             {
  88.                 for (auto& in : vTest)
  89.                 {
  90.                     if (in.included)
  91.                     {
  92.                         consumeValue(in.value);
  93.                         in.included = false;
  94.                         break;
  95.                     }
  96.                 }
  97.             }
  98.  
  99.             const auto duration = timer.Duration();
  100.             core::Log::OutInfoFmt("List-search took {}s", duration);
  101.         }
  102.     }
  103.  
  104.     // implicit list - separately stored
  105.     {
  106.         sys::Vector<sys::Pointer<X2>> vTest(NUM, sys::ReserveHint);
  107.  
  108.         for (const auto i : sys::iterateTo(NUM))
  109.         {
  110.             vTest.EmplaceBack(sys::makePointer<X2>(generateValue(), sys::Random::Chance(CHANCE)));
  111.         }
  112.  
  113.         // being 'fair' here => creation of the list, if the elements already exists, does not include the time it takes to create the elements
  114.         core::Timer timer;
  115.  
  116.         sys::Random::InitSeed(SEED);
  117.  
  118.         X2* pLast = nullptr;
  119.         for (X2* pX : vTest)
  120.         {
  121.             if (pLast)
  122.                 pLast->next = pX;
  123.             pLast = pX;
  124.         }
  125.  
  126.         core::Log::OutInfoFmt("Implicit list-creation took {}s", timer.Duration());
  127.  
  128.         {
  129.             timer.Reset();
  130.             for (const auto i : sys::iterateTo(ITERATIONS))
  131.             {
  132.                 X2* pX = vTest.Front();
  133.                 while (true)
  134.                 {
  135.                     if (pX->included)
  136.                     {
  137.                         consumeValue(pX->value);
  138.                         pX->included = false;
  139.                         break;
  140.                     }
  141.  
  142.                     pX = pX->next;
  143.                     if (!pX)
  144.                         break;
  145.                 };
  146.             }
  147.  
  148.             const auto duration = timer.Duration();
  149.             core::Log::OutInfoFmt("Implicit list-search took {}s", duration);
  150.         }
  151.     }
  152.  
  153.     // implicit list - inline stored
  154.     {
  155.         sys::Vector<X2> vTest(NUM, sys::ReserveHint);
  156.  
  157.         for (const auto i : sys::iterateTo(NUM))
  158.         {
  159.             vTest.EmplaceBack(generateValue(), sys::Random::Chance(CHANCE));
  160.         }
  161.  
  162.         // being 'fair' here => creation of the list, if the elements already exists, does not include the time it takes to create the elements
  163.         core::Timer timer;
  164.  
  165.         sys::Random::InitSeed(SEED);
  166.  
  167.         X2* pLast = nullptr;
  168.         for (X2& x : vTest)
  169.         {
  170.             if (pLast)
  171.                 pLast->next = &x;
  172.             pLast = &x;
  173.         }
  174.  
  175.         core::Log::OutInfoFmt("Implicit inline list-creation took {}s", timer.Duration());
  176.  
  177.         {
  178.             timer.Reset();
  179.             for (const auto i : sys::iterateTo(ITERATIONS))
  180.             {
  181.                 X2* pX = &vTest.Front();
  182.                 while (true)
  183.                 {
  184.                     if (pX->included)
  185.                     {
  186.                         consumeValue(pX->value);
  187.                         pX->included = false;
  188.                         break;
  189.                     }
  190.  
  191.                     pX = pX->next;
  192.                     if (!pX)
  193.                         break;
  194.                 };
  195.             }
  196.  
  197.             const auto duration = timer.Duration();
  198.             core::Log::OutInfoFmt("Implicit inline list-search took {}s", duration);
  199.         }
  200.     }
  201.  
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement