Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
#include <cstdlib> #include <iostream> #include <windows.h> #include <time.h> int Data = 0; int Data2 = 0; class Branch { public: void Work(const bool Arg1, const bool Arg2, const int Iterations) { for (int n = 0; n < Iterations; ++n) { if (Arg1) { Data = Data2; Data2 += 1;; } if (Arg2) { Data = Data2; Data2 += 2;; } } }; }; template<const bool Arg1, const bool Arg2> class Template { public: void Work(const int Iterations) { for (int n = 0; n < Iterations; ++n) { if (Arg1) { Data = Data2; Data2 += 1;; } if (Arg2) { Data = Data2; Data2 += 2;; } } }; }; int main(int argc, wchar_t* argv[]) { //testobjekte Template<false, false> TemplateObj0; Template<true, false> TemplateObj1; Template<false, true> TemplateObj2; Template<true, true> TemplateObj3; Branch BranchObj; //einstellungen (volatile verhindert optimierungen) volatile bool Arg1 = false; volatile bool Arg2 = true; volatile int Iterations = 1000000000; volatile int TemplateNum = (int)Arg1 + ((int)Arg2 * 2); std::cout << "TemplateNum" << TemplateNum << "\n"; //performance messungs variablen LONGLONG g_Frequency; QueryPerformanceFrequency((LARGE_INTEGER*)&g_Frequency); //test branch { LONGLONG g_CurentCount, g_LastCount; QueryPerformanceCounter((LARGE_INTEGER*)&g_CurentCount); Data = 0; Data2 = 0; BranchObj.Work(Arg1, Arg2, Iterations); QueryPerformanceCounter((LARGE_INTEGER*)&g_LastCount); double dTimeDiff = (((double)(g_LastCount - g_CurentCount)) / ((double)g_Frequency)); std::cout << dTimeDiff << "\n"; std::cout << "Result " << Data << "\n"; } //test template { LONGLONG g_CurentCount, g_LastCount; QueryPerformanceCounter((LARGE_INTEGER*)&g_CurentCount); Data = 0; Data2 = 0; switch (TemplateNum) { case 0: TemplateObj0.Work(Iterations); break; case 1: TemplateObj1.Work(Iterations); break; case 2: TemplateObj2.Work(Iterations); break; case 3: TemplateObj3.Work(Iterations); break; } QueryPerformanceCounter((LARGE_INTEGER*)&g_LastCount); double dTimeDiff = (((double)(g_LastCount - g_CurentCount)) / ((double)g_Frequency)); std::cout << dTimeDiff << "\n"; std::cout << "Result " << Data << "\n"; } std::getchar(); return 0; } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Horus« (28.04.2014, 08:25)
C-/C++-Quelltext |
|
1 2 3 4 |
int main(int _argc, char** _argv) noexcept { asm volatile("lock cmpxchg8b %eax"); return 0; } // ::main |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Administrator
Zitat
Konstante Ausdrücke und Bedingungen kann der Compiler selber wegoptimieren. Das von Hand zu tun ist entsprechend unnötig.
Zitat
Wenn sich Arg1 oder Arg2 während der Iterationen ändert, wird bei der nächsten Iteration der neue Wert benutzt. In deinem Template Code ist der Branch (das switch-case) außerhalb der Schleife.
Zitat
Du verhinderst also Optimierungen und wunderst dich dann, dass es nicht optimal läuft
Werbeanzeige