0

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.

  • 1
    Questions that contain weasel words like "better" are unanswerable. We don't know what "better" means to you specifically. – Robert Harvey Jan 12 '17 at 19:05
  • @RobertHarvey I updated my question now .. thank you for the suggestion. – solti Jan 12 '17 at 19:11
  • Better. So what is your actual question? – Robert Harvey Jan 12 '17 at 19:13
  • @RobertHarvey "So what I wanted was to define the measure_time class only once and use it across the class A, B and C." my concern is that I am defining measure_time class in each class A, B and C and wondering if that is right approach. I think defining class measure_time once and reusing it better approach. So I guess my question is how to do it in a way I only have to define class measure_time once and re-use it. – solti Jan 12 '17 at 19:21
  • @RobertHarvey let me know if I am still not struggling to explain my question :p because it has happened to me before .. and I want to learn a proper approach to ask question. – solti Jan 12 '17 at 19:23
2

t seems as if you're trying to use one timer to measure execution time across three objects? If that's what you're doing, your classes shouldn't know anything about that timer.

Set up a timer in another class, start it, execute your three objects, stop the timer, and see the result. An object should only have its own timer if it's responsible for timing itself.

  • I am still having difficulty understanding one this. When I do 1) starting timer 2) execute three objects 3) stop timer. Lets say one of class's function (A) calls other (B,C)class functions. Then from main I execute A's function that class other (B,C)class function. How can I measure the execution time of other (B,C)class function since this measurement from main will only give time of A's function which is combination of time executing B and C's function . eg : A () { B.g(); C.h();} – solti Jan 12 '17 at 21:13
  • If class R containing functions B and C needs to be timed independently, then put a timer into class R. Wouldn't it be easier to just run a profiler on this code? – Robert Harvey Jan 12 '17 at 21:24
  • 1
    that is correct .. I got carried away for sec there – solti Jan 12 '17 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.