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
|
#include <iostream>
#include <functional>
#include <chrono>
#include <ctime>
using std::function;
using namespace std::chrono;
using namespace std::chrono_literals;
enum MyEnum
{
First,
Second,
Third,
Fourth,
Fifth,
Sixth,
Seventh,
Eigth,
Ninth,
Tenth,
Eleventh,
NUM_ELEMENTS
};
volatile unsigned long long n = 0 ;
struct LambdaOp
{
function< void() > op = []() { std::cout << "Why?\n"; };
};
static const LambdaOp s_LambdaOps[ NUM_ELEMENTS ] =
{
{ []() { std::cout << "adummy\n"; } }, // First
{ []() { std::cout << "bdummy\n"; } }, // Second
{ []() { std::cout << "cdummy\n"; } }, // Third
{ []() { std::cout << "ddummy\n"; } }, // Fourth
{ []() { std::cout << "edummy\n"; } }, // Fifth
{ []() { ++n ; } }, // Sixth
{ []() { std::cout << "gdummy\n"; } }, // Seventh
{ []() { std::cout << "hdummy\n"; } }, // Eigth
{ []() { std::cout << "idummy\n"; } }, // Ninth
{ []() { std::cout << "jdummy\n"; } }, // Tenth
{ []() { std::cout << "kdummy\n"; } }, // Eleventh
};
void LambdaMethod( const MyEnum &lambdaOp )
{
s_LambdaOps[ lambdaOp ].op();
}
void SwitchMethod( const MyEnum &statement )
{
switch ( statement )
{
case First:
std::cout << "adummy\n";
break;
case Second:
std::cout << "bdummy\n";
break;
case Third:
std::cout << "cdummy\n";
break;
case Fourth:
std::cout << "ddummy\n";
break;
case Fifth:
std::cout << "edummy\n";
break;
case Sixth:
++n ;
break;
case Seventh:
std::cout << "gdummy\n";
break;
case Eigth:
std::cout << "hdummy\n";
break;
case Ninth:
std::cout << "idummy\n";
break;
case Tenth:
std::cout << "jdummy\n";
break;
case Eleventh:
std::cout << "kdummy\n";
break;
default:
std::cout << "Why?\n";
break;
}
}
class MyTimer
{
public:
void Start()
{
start = std::clock() ;
}
void Stop()
{
end = std::clock() ;
}
double GetTime() const
{
return (end-start) * 1000.0 / CLOCKS_PER_SEC ;
}
private:
std::clock_t start = std::clock();
std::clock_t end = std::clock() ;
};
int main()
{
std::cout << std::fixed ;
const long long numIterations = 400'000'000;
const MyEnum element = Sixth;
// get relevant stuff into the cache
SwitchMethod( element );
LambdaMethod( element );
MyTimer SwitchTimer = {};
MyTimer LambdaTimer = {};
SwitchTimer.Start();
for ( long long i = 0; i < numIterations; ++i )
{
SwitchMethod( element );
}
SwitchTimer.Stop();
std::cout << "Switch Time: " << SwitchTimer.GetTime() << " (processor time, milliseconds)\n";
LambdaTimer.Start();
for ( long long i = 0; i < numIterations; ++i )
{
LambdaMethod( element );
}
LambdaTimer.Stop();
std::cout << "Lambda Time: " << LambdaTimer.GetTime() << " (processor time, milliseconds)\n";
LambdaTimer.Start();
for ( long long i = 0; i < numIterations; ++i )
{
LambdaMethod( element );
}
LambdaTimer.Stop();
std::cout << "Lambda Time: " << LambdaTimer.GetTime() << " (processor time, milliseconds)\n";
SwitchTimer.Start();
for ( long long i = 0; i < numIterations; ++i )
{
SwitchMethod( element );
}
SwitchTimer.Stop();
std::cout << "Switch Time: " << SwitchTimer.GetTime() << " (processor time, milliseconds)\n";
}
| |