Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <cstdint>
- #include <Windows.h>
- #include <vector>
- //======================================================
- //Дабы избежать оптимизаций компилятора
- uint64_t gGlobalVar = 0;
- DECLSPEC_NOINLINE void Inc5()
- {
- gGlobalVar += 5;
- }
- DECLSPEC_NOINLINE void Inc7()
- {
- gGlobalVar += 7;
- }
- //======================================================
- std::vector<uint8_t> GenRandomVec(size_t size)
- {
- std::random_device rd;
- std::mt19937_64 mt(rd());
- std::vector<uint8_t> constVec(size);
- std::uniform_int_distribution<int> distrib;
- for (uint64_t i = 0; i < constVec.size(); i++)
- {
- constVec[i] = distrib(mt) & 0xff;
- }
- return std::move(constVec);
- }
- //======================================================
- double BranchedTime(int64_t timeFreq, uint8_t threshold, const std::vector<uint8_t>& randomConditions)
- {
- int64_t start, end;
- QueryPerformanceCounter((LARGE_INTEGER*)(&start));
- for (uint64_t i = 0; i < randomConditions.size(); i++)
- {
- if (randomConditions[i] > threshold)
- {
- Inc5();
- }
- else
- {
- Inc7();
- }
- }
- QueryPerformanceCounter((LARGE_INTEGER*)(&end));
- return (double)(end - start) / timeFreq;
- }
- double UnbranchedTime(int64_t timeFreq, uint8_t threshold, const std::vector<uint8_t>& randomConditions)
- {
- int64_t start, end;
- void(*Incs[2])() = { Inc7, Inc5 };
- QueryPerformanceCounter((LARGE_INTEGER*)(&start));
- for (uint64_t i = 0; i < randomConditions.size(); i++)
- {
- Incs[randomConditions[i] > threshold]();
- }
- QueryPerformanceCounter((LARGE_INTEGER*)(&end));
- return (double)(end - start) / timeFreq;
- }
- //======================================================
- int main()
- {
- uint8_t threshold = 127;
- std::vector<uint8_t> cVec = std::move(GenRandomVec(1000000000)); //1 billion
- int64_t freq;
- QueryPerformanceFrequency((LARGE_INTEGER*)(&freq));
- double bTime = BranchedTime(freq, threshold, cVec);
- double uTime = UnbranchedTime(freq, threshold, cVec);
- std::cout << "Branched time = " << bTime << std::endl
- << "Unbranched time = " << uTime << std::endl;
- std::cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment