SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <iostream> | |
| 2 | #include <random> | |
| 3 | #include <cstdint> | |
| 4 | #include <Windows.h> | |
| 5 | #include <vector> | |
| 6 | ||
| 7 | //====================================================== | |
| 8 | //Дабы избежать оптимизаций компилятора | |
| 9 | ||
| 10 | uint64_t gGlobalVar = 0; | |
| 11 | ||
| 12 | DECLSPEC_NOINLINE void Inc5() | |
| 13 | {
| |
| 14 | gGlobalVar += 5; | |
| 15 | } | |
| 16 | ||
| 17 | DECLSPEC_NOINLINE void Inc7() | |
| 18 | {
| |
| 19 | gGlobalVar += 7; | |
| 20 | } | |
| 21 | ||
| 22 | //====================================================== | |
| 23 | ||
| 24 | std::vector<uint8_t> GenRandomVec(size_t size) | |
| 25 | {
| |
| 26 | std::random_device rd; | |
| 27 | std::mt19937_64 mt(rd()); | |
| 28 | ||
| 29 | std::vector<uint8_t> constVec(size); | |
| 30 | std::uniform_int_distribution<int> distrib; | |
| 31 | for (uint64_t i = 0; i < constVec.size(); i++) | |
| 32 | {
| |
| 33 | constVec[i] = distrib(mt) & 0xff; | |
| 34 | } | |
| 35 | ||
| 36 | return std::move(constVec); | |
| 37 | } | |
| 38 | ||
| 39 | //====================================================== | |
| 40 | ||
| 41 | double BranchedTime(int64_t timeFreq, uint8_t threshold, const std::vector<uint8_t>& randomConditions) | |
| 42 | {
| |
| 43 | int64_t start, end; | |
| 44 | ||
| 45 | QueryPerformanceCounter((LARGE_INTEGER*)(&start)); | |
| 46 | for (uint64_t i = 0; i < randomConditions.size(); i++) | |
| 47 | {
| |
| 48 | if (randomConditions[i] > threshold) | |
| 49 | {
| |
| 50 | Inc5(); | |
| 51 | } | |
| 52 | else | |
| 53 | {
| |
| 54 | Inc7(); | |
| 55 | } | |
| 56 | } | |
| 57 | QueryPerformanceCounter((LARGE_INTEGER*)(&end)); | |
| 58 | ||
| 59 | return (double)(end - start) / timeFreq; | |
| 60 | } | |
| 61 | ||
| 62 | double UnbranchedTime(int64_t timeFreq, uint8_t threshold, const std::vector<uint8_t>& randomConditions) | |
| 63 | {
| |
| 64 | int64_t start, end; | |
| 65 | ||
| 66 | void(*Incs[2])() = { Inc7, Inc5 };
| |
| 67 | ||
| 68 | QueryPerformanceCounter((LARGE_INTEGER*)(&start)); | |
| 69 | for (uint64_t i = 0; i < randomConditions.size(); i++) | |
| 70 | {
| |
| 71 | Incs[randomConditions[i] > threshold](); | |
| 72 | } | |
| 73 | QueryPerformanceCounter((LARGE_INTEGER*)(&end)); | |
| 74 | ||
| 75 | return (double)(end - start) / timeFreq; | |
| 76 | } | |
| 77 | ||
| 78 | //====================================================== | |
| 79 | ||
| 80 | int main() | |
| 81 | {
| |
| 82 | uint8_t threshold = 127; | |
| 83 | std::vector<uint8_t> cVec = std::move(GenRandomVec(1000000000)); //1 billion | |
| 84 | ||
| 85 | int64_t freq; | |
| 86 | QueryPerformanceFrequency((LARGE_INTEGER*)(&freq)); | |
| 87 | ||
| 88 | double bTime = BranchedTime(freq, threshold, cVec); | |
| 89 | double uTime = UnbranchedTime(freq, threshold, cVec); | |
| 90 | ||
| 91 | std::cout << "Branched time = " << bTime << std::endl | |
| 92 | << "Unbranched time = " << uTime << std::endl; | |
| 93 | ||
| 94 | std::cin.get(); | |
| 95 | return 0; | |
| 96 | } |