how to identify the number of calls of a fuction

Discussion in 'C++' started by contactmayankjain@gmail.com, Aug 7, 2007.

  1. Guest

    Hi,


    Can you tell me is there some procedure to calculate the number of
    calls of a function during run time?
    How to insert a counter and how to increment it? and possibly which
    function have called it how many times?



    Thanks
    Regards
    Mayank Jain
    +919818390836
     
    , Aug 7, 2007
    #1
    1. Advertising

  2. wrote:
    > Can you tell me is there some procedure to calculate the number of
    > calls of a function during run time?


    Yes, it's called "call counter". You place

    unsigned my_function_counter;

    right before the function, in the global scope. Then the very first
    statement in the function should be

    ++my_function_counter;

    and then at the end of your program you simply print the counter out.

    > How to insert a counter and how to increment it? and possibly which
    > function have called it how many times?


    Ah... Which function... You need a profiler. Just use the proper
    tool for the job.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 7, 2007
    #2
    1. Advertising

  3. On 2007-08-07 19:21, Victor Bazarov wrote:
    > wrote:
    >> Can you tell me is there some procedure to calculate the number of
    >> calls of a function during run time?

    >
    > Yes, it's called "call counter". You place
    >
    > unsigned my_function_counter;
    >
    > right before the function, in the global scope. Then the very first
    > statement in the function should be
    >
    > ++my_function_counter;
    >
    > and then at the end of your program you simply print the counter out.
    >
    >> How to insert a counter and how to increment it? and possibly which
    >> function have called it how many times?

    >
    > Ah... Which function... You need a profiler. Just use the proper
    > tool for the job.


    Or, a bit more cumbersome, identify all places a call is made to your
    function (any good IDE should be able to tell you, along with some other
    tools) and add counters at each call-point. But if you need to do this
    for more than one function then you probably should get a profiler.

    BTW: A bit off-topic but, is there any good, free profiler for windows.
    This seems to be one of those where you can only get two out of three.
    Either it's good but not free, free but not good, or not for windows.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 7, 2007
    #3
  4. Erik Wikström wrote:
    > [..]
    > BTW: A bit off-topic but, is there any good, free profiler for
    > windows.


    BTW, a bit off-topic, but, wouldn't it be too much to ask to use
    proper punctuation? You know, like the question mark I put after
    I asked a question...

    "Good" and "free" rarely coexist in a characterisation of a piece
    of software, especially when development tools are concerned.
    Just get a trial version of AQtime, you get two weeks of use, it
    should be enough to learn the tool and to improve the performance
    of your program. Then buy it. Don't get cheap when tools are
    concerned. A good tool is worth every penny.

    > This seems to be one of those where you can only get two out
    > of three. Either it's good but not free, free but not good, or not
    > for windows.


    Yep.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 7, 2007
    #4
  5. Shadowman Guest

    Victor Bazarov wrote:
    > wrote:
    >> Can you tell me is there some procedure to calculate the number of
    >> calls of a function during run time?

    >
    > Yes, it's called "call counter". You place
    >
    > unsigned my_function_counter;
    >
    > right before the function, in the global scope. Then the very first
    > statement in the function should be
    >
    > ++my_function_counter;
    >
    > and then at the end of your program you simply print the counter out.
    >


    Why not a static variable inside the function?


    --
    SM
    rot13 for email
     
    Shadowman, Aug 7, 2007
    #5
  6. Shadowman wrote:
    > Victor Bazarov wrote:
    >> wrote:
    >>> Can you tell me is there some procedure to calculate the number of
    >>> calls of a function during run time?

    >>
    >> Yes, it's called "call counter". You place
    >>
    >> unsigned my_function_counter;
    >>
    >> right before the function, in the global scope. Then the very first
    >> statement in the function should be
    >>
    >> ++my_function_counter;
    >>
    >> and then at the end of your program you simply print the counter out.
    >>

    >
    > Why not a static variable inside the function?


    How would you print it out?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 7, 2007
    #6
  7. terminator Guest

    On Aug 7, 9:23 pm, Shadowman <> wrote:
    > Victor Bazarov wrote:
    > > wrote:
    > >> Can you tell me is there some procedure to calculate the number of
    > >> calls of a function during run time?

    >
    > > Yes, it's called "call counter". You place

    >
    > > unsigned my_function_counter;

    >
    > > right before the function, in the global scope. Then the very first
    > > statement in the function should be

    >
    > > ++my_function_counter;

    >
    > > and then at the end of your program you simply print the counter out.

    >
    > Why not a static variable inside the function?


    you will have to either return that variable by value or store it in a
    global variable before return in which case simply using a global is
    more efficient.

    regards,
    FM.
     
    terminator, Aug 7, 2007
    #7
  8. terminator Guest

    On Aug 7, 8:21 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > Can you tell me is there some procedure to calculate the number of
    > > calls of a function during run time?

    >
    > Yes, it's called "call counter". You place
    >
    > unsigned my_function_counter;
    >
    > right before the function, in the global scope. Then the very first
    > statement in the function should be
    >
    > ++my_function_counter;
    >
    > and then at the end of your program you simply print the counter out.
    >
    > > How to insert a counter and how to increment it? and possibly which
    > > function have called it how many times?

    >
    > Ah... Which function... You need a profiler. Just use the proper
    > tool for the job.
    >
    > V


    you can also get a pointer to caller as an argument of the callee
    which is then stored in a container:

    typedef std::pair<void(*)(void),std::string> mypair;

    inline std::vector< mypair >& call_map(){
    static std::vector< mypair > data;
    return data;
    };

    void callee(void (* mycaller)(void),std::string caller_name){

    call_map().push_back(mypair(mycaller,caller_name));
    ...//go on
    };

    void caller(void){
    callee(&caller,"caller");

    };

    void print(const mypair& mp){
    //do some printing on mp
    };

    int main (void){
    caller();
    std::cout<<"callee count="<<call_map().size()<<endl;
    std::for_each(call_map().begin(),call_map.end(),print);
    return 0;
    };

    regards,
    FM.
     
    terminator, Aug 7, 2007
    #8
  9. James Kanze Guest

    On Aug 7, 7:58 pm, "Victor Bazarov" <> wrote:
    > Erik Wikström wrote:
    > > [..]
    > > BTW: A bit off-topic but, is there any good, free profiler for
    > > windows.


    > "Good" and "free" rarely coexist in a characterisation of a piece
    > of software, especially when development tools are concerned.


    I tend to agree, although there are notable exceptions,
    precisely in the domain of development tools: g++ and gprof.
    Also, regrettably "good" and "commercial" rarely coexist either.

    Logically, commercial software has definite advantages: the
    additional leverage over employees can't hurt (although abusing
    it is not the best way to get the process to work).
    Practically, hackers seem to abound in both commercial and free
    software, and even acceptable quality is the exception for both.
    And leverage or not, some non-commercial operations, like g++,
    have managed to implement a fairly good development
    process---better, at any rate, than that in most commercial
    organizations.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 8, 2007
    #9
  10. James Kanze Guest

    On Aug 7, 7:16 pm, ""
    <> wrote:

    > Can you tell me is there some procedure to calculate the number of
    > calls of a function during run time?
    > How to insert a counter and how to increment it? and possibly which
    > function have called it how many times?


    It's implementation dependent, but any good compiler should have
    an option to do this automatically, writing the results out to a
    file at the end of execution. (With g++, for example, the
    option is -pg. And the output is written in a binary format, so
    you need an additional tool, gprof, to read it. On the other
    hand, you get a lot more than just the counts of how many times
    each function has been called.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 8, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    3
    Views:
    387
    Chris Botha
    Nov 16, 2005
  2. somez72
    Replies:
    4
    Views:
    4,404
    Roedy Green
    May 3, 2004
  3. MiniDisc_2k2
    Replies:
    2
    Views:
    404
    MiniDisc_2k2
    Jul 3, 2003
  4. orion30
    Replies:
    2
    Views:
    333
    Alf P. Steinbach
    Jul 14, 2003
  5. Michelle
    Replies:
    8
    Views:
    172
    Chris Barber
    Aug 2, 2003
Loading...

Share This Page