/* * GCC: * * g++ -std=c++11 -O3 -faggressive-loop-optimizations \ * -funsafe-loop-optimizations -ftree-loop-vectorize test.cpp * * 22971931us * 23148905us * 23087770us * 0us * * clang: * * clang++ --std=c++11 -O3 test.cpp * * 23586034us * 23627601us * 26344500us * 2711us * * */ #include #include #include using namespace std; using namespace std::chrono; class Test { static const int Iterations = 1000000; public: static void Main() { Measure(&ByteLoop); Measure(&ShortLoop); Measure(&IntLoop); //Measure(BackToBack); Measure(&DelegateOverhead); } private: //delegate -> pointer to function // void (*foo)(); static void Measure(void (*action)(void) ) { //GC.Collect(); //GC.WaitForPendingFinalizers(); //GC.Collect(); // Stopwatch sw = Stopwatch.StartNew(); high_resolution_clock::time_point t1 = high_resolution_clock::now(); for (int i = 0; i < Iterations; i++) { action(); } //sw.Stop(); high_resolution_clock::time_point t2 = high_resolution_clock::now(); //Console.WriteLine("{0}: {1}ms", action.Method.Name, // sw.ElapsedMilliseconds); auto duration = duration_cast( t2 - t1 ).count(); cout << duration << "us" << endl; } static void ByteLoop(void) { for (char index = 0; index < 127; index++) { //Iterations.ToString(); to_string(index); } } static void ShortLoop(void) { for (short index = 0; index < 127; index++) { //Iterations.ToString(); to_string(index); } } static void IntLoop(void) { for (int index = 0; index < 127; index++) { //Iterations.ToString(); to_string(index); } } static void BackToBack() { for (char index = 0; index < 127; index++) { //Iterations.ToString(); to_string(index); } for (short index = 0; index < 127; index++) { //Iterations.ToString(); to_string(index); } for (int index = 0; index < 127; index++) { //Iterations.ToString(); to_string(index); } } static void DelegateOverhead() { // Nothing. Let's see how much // overhead there is just for calling // this repeatedly... } }; int main() { Test::Main(); return 0; }