/* * Copyright 2010 Tino Didriksen * http://tinodidriksen.com/ */ #ifdef _MSC_VER #define _SECURE_SCL 0 #define _CRT_SECURE_NO_DEPRECATE 1 #define WIN32_LEAN_AND_MEAN #define VC_EXTRALEAN #define NOMINMAX #endif #include #include #include #include #include #include #include #include #include "cycle.h" const size_t N = 1000000; const size_t R = 7; std::vector numbers; void PrintStats(std::vector timings) { double fastest = std::numeric_limits::max(); std::cout << std::fixed << std::setprecision(2); std::cout << "["; for (size_t i = 1 ; i > timings(17); for (size_t r=0 ; r(ones[3]); res += a->getType(); } end = getticks(); timed = elapsed(end, start); timings[0].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; igetType() == T_ONE_LEVEL2) { OneLevel2 *a = reinterpret_cast(ones[2]); res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[1].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[0]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[2].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[1]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[3].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[2]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[4].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[3]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[5].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[1]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[6].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[2]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[7].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[3]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[8].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[2]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[9].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[3]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[10].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[3]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[11].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[0]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[12].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[1]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[13].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[2]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[14].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; i(ones[3]); if (a) { res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[15].push_back(timed); std::cerr << res << std::endl; res = 0; start = getticks(); for (size_t i=0 ; itype == T_ONE_LEVEL3) { OneLevel3 *a = reinterpret_cast(ones[3]); res += a->getType(); } } end = getticks(); timed = elapsed(end, start); timings[16].push_back(timed); std::cerr << res << std::endl; std::cerr << std::endl; } std::cout << "reinterpret_cast known-type: "; PrintStats(timings[0]); std::cout << std::endl; std::cout << "virtual function + reinterpret_cast: "; PrintStats(timings[1]); std::cout << std::endl; std::cout << "member variable + reinterpret_cast: "; PrintStats(timings[16]); std::cout << std::endl; std::cout << "dynamic_cast same-type-base success: "; PrintStats(timings[2]); std::cout << std::endl; std::cout << "dynamic_cast same-type-level1 success: "; PrintStats(timings[3]); std::cout << std::endl; std::cout << "dynamic_cast same-type-level2 success: "; PrintStats(timings[4]); std::cout << std::endl; std::cout << "dynamic_cast same-type-level3 success: "; PrintStats(timings[5]); std::cout << std::endl; std::cout << "dynamic_cast level1-to-base success: "; PrintStats(timings[6]); std::cout << std::endl; std::cout << "dynamic_cast level2-to-base success: "; PrintStats(timings[7]); std::cout << std::endl; std::cout << "dynamic_cast level3-to-base success: "; PrintStats(timings[8]); std::cout << std::endl; std::cout << "dynamic_cast level2-to-level1 success: "; PrintStats(timings[9]); std::cout << std::endl; std::cout << "dynamic_cast level3-to-level1 success: "; PrintStats(timings[10]); std::cout << std::endl; std::cout << "dynamic_cast level3-to-level2 success: "; PrintStats(timings[11]); std::cout << std::endl; std::cout << "dynamic_cast onebase-to-twobase fail: "; PrintStats(timings[12]); std::cout << std::endl; std::cout << "dynamic_cast onelevel1-to-twobase fail: "; PrintStats(timings[13]); std::cout << std::endl; std::cout << "dynamic_cast onelevel2-to-twobase fail: "; PrintStats(timings[14]); std::cout << std::endl; std::cout << "dynamic_cast onelevel3-to-twobase fail: "; PrintStats(timings[15]); std::cout << std::endl; std::cout << std::endl; }