Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct X
- {
- int value;
- bool included;
- };
- static const auto v = sys::Random::Value<int>();
- __declspec(noinline) void consumeValue(int x)
- {
- if (!v) [[unlikely]]
- core::Log::OutInfoFmt("{}", x);
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- constexpr auto NUM = 10000000;
- constexpr auto ITERATIONS = 5000;
- constexpr auto SEED = 10000;
- constexpr auto CHANCE = 0.001f;
- const auto generateValue = [&]
- {
- return sys::Random::Max<int>(100000);
- };
- // vector
- {
- core::Timer timer;
- sys::Vector<X, sys::CompactSizeT> vTest(NUM, sys::ReserveHint);
- sys::Random::InitSeed(SEED);
- for (const auto i : sys::iterateTo(NUM))
- {
- vTest.EmplaceBackReserved(generateValue(), sys::Random::Chance(CHANCE));
- }
- core::Log::OutInfoFmt("Vector-creation took {}s", timer.Duration());
- {
- timer.Reset();
- for (const auto i : sys::iterateTo(ITERATIONS))
- {
- for (auto& in : vTest)
- {
- if (in.included)
- {
- consumeValue(in.value);
- in.included = false;
- break;
- }
- }
- }
- const auto duration = timer.Duration();
- core::Log::OutInfoFmt("Vector-search took {}s", duration);
- }
- }
- // list
- {
- core::Timer timer;
- std::list<X> vTest;
- sys::Random::InitSeed(SEED);
- for (const auto i : sys::iterateTo(NUM))
- {
- vTest.emplace_back(generateValue(), sys::Random::Chance(CHANCE));
- }
- core::Log::OutInfoFmt("List-creation took {}s", timer.Duration());
- {
- timer.Reset();
- for (const auto i : sys::iterateTo(ITERATIONS))
- {
- for (auto& in : vTest)
- {
- if (in.included)
- {
- consumeValue(in.value);
- in.included = false;
- break;
- }
- }
- }
- const auto duration = timer.Duration();
- core::Log::OutInfoFmt("List-search took {}s", duration);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement