C++ (Qt) std::vector< std::vector<int> > arr( 5 );
C++ (Qt)/******************************************************************************** file :* created : 30.10.2014* author :*******************************************************************************/ #include <iostream>#include <vector>#include <array>#include <chrono> #include <cstdint> using namespace std::chrono; template<class T, uint32_t N >inline uint32_t ARRAY_SIZE(T (&)[N]) {return N;} static const float g_quad_vertex_buffer_data[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f,}; static const std::vector<float> v = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f,}; static const std::array<float, 18> a = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f,}; double make_sum(const float* data, std::size_t sz); template <typename T>double make_sum_inl(const T& vec){ double res = 0.; for(std::size_t i = 0; i < vec.size(); ++i) res += vec.at(i); return res;} double make_sum_inl(const float* data, std::size_t sz){ double res = 0.; for(std::size_t i = 0; i < sz; ++i) res += data[i]; return res;} int main(int argc, char* argv[]){ std::cout << "Hello ..." << std::endl; std::cout << "Sum 1 = " << make_sum(g_quad_vertex_buffer_data,ARRAY_SIZE(g_quad_vertex_buffer_data)) << std::endl; std::cout << "Sum 2 = " << make_sum(v.data(),v.size()) << std::endl; static const uint64_t iter_cnt = 100000000; { high_resolution_clock::time_point t1 = high_resolution_clock::now(); volatile double s; const std::size_t add_sz = ARRAY_SIZE(g_quad_vertex_buffer_data); for(uint64_t i = 0; i < iter_cnt; i ++) s = make_sum(g_quad_vertex_buffer_data, add_sz); high_resolution_clock::time_point t2 = high_resolution_clock::now(); std::cout << "C array (noinline)\t "; std::cout << duration_cast<nanoseconds>(t2 - t1).count() ; std::cout << std::endl; } { high_resolution_clock::time_point t1 = high_resolution_clock::now(); volatile double s; for(uint64_t i = 0; i < iter_cnt; i ++) s = make_sum(v.data(),v.size()); high_resolution_clock::time_point t2 = high_resolution_clock::now(); std::cout << "Vector (noinline)\t " ; std::cout << duration_cast<nanoseconds>(t2 - t1).count() ; std::cout << std::endl; } { high_resolution_clock::time_point t1 = high_resolution_clock::now(); volatile double s; for(uint64_t i = 0; i < iter_cnt; i ++) s = make_sum(a.data(),a.size()); high_resolution_clock::time_point t2 = high_resolution_clock::now(); std::cout << "Array (noinline)\t " ; std::cout << duration_cast<nanoseconds>(t2 - t1).count() ; std::cout << std::endl; }//====================================================================================================== { high_resolution_clock::time_point t1 = high_resolution_clock::now(); volatile double s; const std::size_t add_sz = ARRAY_SIZE(g_quad_vertex_buffer_data); for(uint64_t i = 0; i < iter_cnt; i ++) s = make_sum_inl(g_quad_vertex_buffer_data,add_sz); high_resolution_clock::time_point t2 = high_resolution_clock::now(); std::cout << "C array (inline)\t " ; std::cout << duration_cast<nanoseconds>(t2 - t1).count() ; std::cout << std::endl; } { high_resolution_clock::time_point t1 = high_resolution_clock::now(); volatile double s; for(uint64_t i = 0; i < iter_cnt; i ++) s = make_sum_inl(v); high_resolution_clock::time_point t2 = high_resolution_clock::now(); std::cout << "Vector (inline)\t " ; std::cout << duration_cast<nanoseconds>(t2 - t1).count() ; std::cout << std::endl; } { high_resolution_clock::time_point t1 = high_resolution_clock::now(); volatile double s; for(uint64_t i = 0; i < iter_cnt; i ++) s = make_sum_inl(a.data(),a.size()); high_resolution_clock::time_point t2 = high_resolution_clock::now(); std::cout << "Array (inline)\t " ; std::cout << duration_cast<nanoseconds>(t2 - t1).count() ; std::cout << std::endl; } return 0;} __attribute__((noinline)) double make_sum(const float* data, std::size_t sz){ double res = 0.; for(std::size_t i = 0; i < sz; ++i) res += data[i]; return res;}
Hello ...C array (noinline) 1062060700Vector (noinline) 1050060100Array (noinline) 1181067600C array (inline) 1262072100Vector (inline) 805046000Array (inline) 796045500
Hello ...Sum 1 = 0Sum 2 = 0Array (noinline) 975055800C array (noinline) 871049800Vector (noinline) 1006057600Array (inline) 666038100C array (inline) 656037500Vector (inline) 660037700Array self (inline) 2067118200C array self (inline) 2101120100Vector self (inline) 21511230400
C++ (Qt)template <typename T>double make_sum_inl( const T& vec){ double res = 0.;// for(std::size_t i = 0; i < vec.size(); ++i)// res += vec.at(i); for (auto it : vec) res += it; return res;}