Advertisement
Guest User

Untitled

a guest
Dec 19th, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. struct X
  2. {
  3.     int value;
  4.     bool included;
  5. };
  6.  
  7. static const auto v = sys::Random::Value<int>();
  8.  
  9. __declspec(noinline) void consumeValue(int x)
  10. {
  11.     if (!v) [[unlikely]]
  12.         core::Log::OutInfoFmt("{}", x);
  13. }
  14.  
  15. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  16. {
  17.     constexpr auto NUM = 10000000;
  18.     constexpr auto ITERATIONS = 5000;
  19.     constexpr auto SEED = 10000;
  20.     constexpr auto CHANCE = 0.001f;
  21.  
  22.     const auto generateValue = [&]
  23.     {
  24.         return sys::Random::Max<int>(100000);
  25.     };
  26.  
  27.     // vector
  28.     {
  29.         core::Timer timer;
  30.  
  31.         sys::Vector<X, sys::CompactSizeT> vTest(NUM, sys::ReserveHint);
  32.  
  33.         sys::Random::InitSeed(SEED);
  34.  
  35.         for (const auto i : sys::iterateTo(NUM))
  36.         {
  37.             vTest.EmplaceBackReserved(generateValue(), sys::Random::Chance(CHANCE));
  38.         }
  39.  
  40.         core::Log::OutInfoFmt("Vector-creation took {}s", timer.Duration());
  41.  
  42.         {
  43.             timer.Reset();
  44.             for (const auto i : sys::iterateTo(ITERATIONS))
  45.             {
  46.                 for (auto& in : vTest)
  47.                 {
  48.                     if (in.included)
  49.                     {
  50.                         consumeValue(in.value);
  51.                         in.included = false;
  52.                         break;
  53.                     }
  54.                 }
  55.             }
  56.  
  57.             const auto duration = timer.Duration();
  58.             core::Log::OutInfoFmt("Vector-search took {}s", duration);
  59.         }
  60.     }
  61.  
  62.     // list
  63.     {
  64.         core::Timer timer;
  65.  
  66.         std::list<X> vTest;
  67.  
  68.         sys::Random::InitSeed(SEED);
  69.  
  70.         for (const auto i : sys::iterateTo(NUM))
  71.         {
  72.             vTest.emplace_back(generateValue(), sys::Random::Chance(CHANCE));
  73.         }
  74.  
  75.         core::Log::OutInfoFmt("List-creation took {}s", timer.Duration());
  76.  
  77.         {
  78.             timer.Reset();
  79.             for (const auto i : sys::iterateTo(ITERATIONS))
  80.             {
  81.                 for (auto& in : vTest)
  82.                 {
  83.                     if (in.included)
  84.                     {
  85.                         consumeValue(in.value);
  86.                         in.included = false;
  87.                         break;
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             const auto duration = timer.Duration();
  93.             core::Log::OutInfoFmt("List-search took {}s", duration);
  94.         }
  95.     }
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement