So I working on a project and want to profile my code. I have been using KCachegrind to get general idea of what functions cost the most. But now I want to get the exact time spent on those particular functions. So I decided to measure them manually using clock_gettime using object oriented approach. ie. wrap clock_gettime function inside a class.
Lets say I want to create a class that handles the measurement of time
class measure_time{
inline int start(){...}; // return ts_start
inline int end(){...}; // return ts_end
};
Then I use this class to measure time across the projects. When I am about to measure I have to create instance of measure_time per each classes of my project. ie: Lets say I have class A, class B, class C etc in my project.
// A.h
class A{
void f();
void f_1();
measure_time mt;
}
// A.cpp
void A::f(){
// does some work
}
void A::f_1(){
// measure time start
s = mt.start();
f()
// measure time end
e = mt.end();
//record time
time = s-e;
}
// B.h
class B{
void g();
void g_1();
measure_time mt;
}
// B.cpp
void B::g(){
// does some work
}
void B::g_1(){
// measure time start
s = mt.start();
g()
// measure time end
e = mt.end();
//record time
time = s-e;
}
// C.h
class C{
void h();
void h_1();
measure_time mt;
}
//C.cpp
void C::h(){
// does some work
}
void C::h_1(){
// measure time start
s = mt.start();
h()
// measure time end
e = mt.end();
//record time
time = s-e;
}
By this approach I have to define 'measure_time' class in each of the class. So what I wanted was to define the measure_time class only once and use it across the class A, B and C.
measure_timeclass in eachclass A, B and Cand wondering if that is right approach. I think definingclass measure_timeonce and reusing it better approach. So I guess my question is how to do it in a way I only have to defineclass measure_timeonce and re-use it.