how measure computation time correctly?
variant 1:
std::chrono::time_point<std::chrono::system_clock> start, end; float elapsed = 0; int n = 100; for(int i=0; i<n; ++i) { start = std::chrono::system_clock::now(); func();//target function end = std::chrono::system_clock::now(); elapsed += std::chrono::duration_cast<std::chrono::microseconds>(end-start).count(); }
variant 2:
start = std::chrono::system_clock::now(); for(int i=0; i<n; ++i) func(); end = std::chrono::system_clock::now(); elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
these variants show different results: tried replace virtual functions std::function :
struct base { virtual void f() = 0; }; struct der1 : public base { virtual void f() override { int i=0; for(int j=0; j<100; ++j) += 2*j; } }; struct der2 : public base { virtual void f() override { int i=0; for(int j=0; j<100; ++j) += 3*j; } }; struct base_ { base_(std::function<void()> f_) : f(f_) {} std::function<void()> f; }; struct der1_ : public base_ { der1_() : base_([]{ int i=0; for(int j=0; j<100; ++j) += 2*j; }){} }; struct der2_ : public base_ { der2_() : base_([]{ int i=0; for(int j=0; j<100; ++j) += 3*j; }){} }; void process1(std::vector<base_*>& v) { for(auto &elem : v) elem->f(); } void process2(std::vector<base*>& v) { for(auto &elem : v) elem->f(); } int main() { std::vector<base_*> vec1; vec1.push_back(new der1_); vec1.push_back(new der2_); vec1.push_back(new der1_); vec1.push_back(new der2_); std::vector<base*> vec2; vec2.push_back(new der1); vec2.push_back(new der2); vec2.push_back(new der1); vec2.push_back(new der2); std::chrono::time_point<std::chrono::system_clock> start1, end1, start2, end2; float elapsed1 = 0; float elapsed2 = 0; int n = 6000; //variant 2 start1 = std::chrono::system_clock::now(); for(int i=0; i<n; ++i) process1(vec1); end1 = std::chrono::system_clock::now(); elapsed1 = std::chrono::duration_cast<std::chrono::microseconds>(end1-start1).count(); start2 = std::chrono::system_clock::now(); for(int i=0; i<n; ++i) process2(vec2); end2 = std::chrono::system_clock::now(); elapsed2 = std::chrono::duration_cast<std::chrono::microseconds>(end2-start2).count(); std::cout<<"virtual: "<<elapsed2<<"\npointer: "<<elapsed1; for(int i=0; i<vec1.size(); ++i) delete vec1[i]; for(int i=0; i<vec2.size(); ++i) delete vec2[i]; return 0; }
and want understand whether performance gains replacement virtual functions on std::function. second variant indicates 2.5-3 gain, while first method shows drop of perfomance.
the reason difference in times time spent doing assignment end
, add time counters. second way avoids @ cost of counting time takes increment i
in loop, less.
Comments
Post a Comment