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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <iostream>
#include <chrono>
using namespace std;
class Timer
{
private:
// Type aliases to make accessing nested type easier
using Clock = std::chrono::steady_clock;
using Second = std::chrono::duration<double, std::ratio<1> >;
std::chrono::time_point<Clock> m_beg{ Clock::now() };
public:
void reset()
{
m_beg = Clock::now();
}
double elapsed() const
{
return std::chrono::duration_cast<Second>(Clock::now() - m_beg).count();
}
};
Timer timer1;
//TEST if/else and switch timing
void TimeIfSwitch(const bool& ifOrSwitch, const bool& state, const int& maxCount, const string msg)
{
unsigned int count{};
if (ifOrSwitch == false) //Test if/else timing
{
timer1.reset();
while (count < maxCount)
{
if (state == true)
++count;
else
++count;
}
}
else //Test switch timing
{
timer1.reset();
while (count < maxCount)
{
switch (state)
{
case true:
++count;
break;
case false:
++count;
break;
}
}
}
//cout << fixed << timer1.elapsed() << msg << endl;
cout << timer1.elapsed() << msg << endl;
cout << "COUNT = " << count << endl;
cout << "***************" << endl;
}
int main()
{
bool stateT = true;
bool stateF = false;
bool testIf = false; //test if/else
bool testSwitch = true; //test switch{}
unsigned int maxCount{1};
cout << "************Test loop 1x************" << endl;
TimeIfSwitch(testIf, stateT, maxCount, " (if (true))");
TimeIfSwitch(testSwitch, stateT, maxCount, " (switch (true))");
TimeIfSwitch(testIf, stateF, maxCount, " (if (false))");
TimeIfSwitch(testSwitch, stateF, maxCount, " (switch (false))");
cout << endl << "************Test loop 1'000'000x************" << endl;
maxCount = 1'000'000;
TimeIfSwitch(testIf, stateT, maxCount, " (if (true))");
TimeIfSwitch(testSwitch, stateT, maxCount, " (switch (true))");
TimeIfSwitch(testIf, stateF, maxCount, " (if (false))");
TimeIfSwitch(testSwitch, stateF, maxCount, " (switch (false))");
cout << endl << "************Test loop 10'000'000x************" << endl;
maxCount = 10'000'000;
TimeIfSwitch(testIf, stateT, maxCount, " (if (true))");
TimeIfSwitch(testSwitch, stateT, maxCount, " (switch (true))");
TimeIfSwitch(testIf, stateF, maxCount, " (if (false))");
TimeIfSwitch(testSwitch, stateF, maxCount, " (switch (false))");
cout << endl << "************Test loop 1x from main()************" << endl;
timer1.reset();
if (stateT == true);
else;
cout << timer1.elapsed() << " (if (true))" << endl;
cout << "COUNT = " << 1 << endl;
cout << "***************" << endl;
timer1.reset();
switch (stateT)
{
case true:
break;
case false:
break;
}
cout << timer1.elapsed() << " (switch (true))" << endl;
cout << "COUNT = " << 1 << endl;
cout << "***************" << endl;
timer1.reset();
if (stateF == true);
else;
cout << timer1.elapsed() << " (if (false))" << endl;
cout << "COUNT = " << 1 << endl;
cout << "***************" << endl;
timer1.reset();
switch (stateF)
{
case true:
break;
case false:
break;
}
cout << timer1.elapsed() << " (switch (false))" << endl;
cout << "COUNT = " << 1 << endl;
cout << "***************" << endl;
return 0;
}
/*
OUTPUT:
************Test loop 1x************
2e-07 (if (true))
COUNT = 1
***************
1e-07 (switch (true))
COUNT = 1
***************
1e-07 (if (false))
COUNT = 1
***************
1e-07 (switch (false))
COUNT = 1
***************
************Test loop 1'000'000x************
0.0010435 (if (true))
COUNT = 1000000
***************
0.0012574 (switch (true))
COUNT = 1000000
***************
0.0010443 (if (false))
COUNT = 1000000
***************
0.0018543 (switch (false))
COUNT = 1000000
***************
************Test loop 10'000'000x************
0.0104584 (if (true))
COUNT = 10000000
***************
0.0126492 (switch (true))
COUNT = 10000000
***************
0.009499 (if (false))
COUNT = 10000000
***************
0.0173167 (switch (false))
COUNT = 10000000
***************
************Test loop 1x from main()************
2e-07 (if (true))
COUNT = 1
***************
1e-07 (switch (true))
COUNT = 1
***************
2e-07 (if (false))
COUNT = 1
***************
2e-07 (switch (false))
COUNT = 1
***************
*/
|