Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Keep track of times function is called

Tags:

c++

Keeping track of how many times a function is called is easy when passing the counter as an argument into the function. It's also easy when returning a one from the called function. But, I do not want to go that route. The reason behind this is because it seems like bad programming (letting the function know too much information). Is there a better way to keep track of how many times this function has been called?

I'm just looking for concepts that I could study. Providing code examples is not neccessary, but might be helpful.

Edit: I'm not actually looking for profiling tools. Let me add some code to get my point across. Because scope for funcCounter ends in main, I have no way of getting back a variable from myFunction that will increment funcCounter. I could possibly return 1 from myFunction and then increment funcCounter that way, but this doesn't seem like very good programming. Is there another way to do it?

int main()
{
int funcCounter = 0;
char *mystring = "This is a silly function.";
myFunction(mystring);
cout << "Times function is called: " << funcCounter << endl;
    return 0;
}

void myFunction(char *mystring)
{
cout << mystring << endl;
}
like image 538
Brundle Avatar asked Jun 14 '10 22:06

Brundle


People also ask

How do you track how many times a function is called?

To count how many times a function has been called, declare a count variable outside of the function, setting it to 0 . Inside of the body of the function reassign the variable incrementing it by 1 . The count variable will store the number of function invocations.

Can a function be called any number of times?

a variable/function can be declared any number of times but it can be defined only once.

How do you count the number of times a function is called in CPP?

The C++ function std::algorithm::count() returns the number of occurrences of value in range. This function uses operator == for comparison.

Which is used to track how many times a function has been called in angular?

count() The console. count() method logs the number of times that this particular call to count() has been called.


2 Answers

Have a static variable in your function and keep incrementing it each time the function in called.

void my_Function(void) {
    static unsigned int call_count = 0;
    call_count++;
}

If you want to do it for debugging reasons, then there are tools like gcov which do this for you. (I'm pretty sure Microsoft doesn't have an alternative bundled with Microsoft Visual C++)

like image 157
aviraldg Avatar answered Oct 05 '22 02:10

aviraldg


I would do this through the use of a profiling tool like gcov (which is for linux). These programs do the work of inserting code into your program during compilation and give you a report of how many times a function is called, where its called from, and how long the program spent executing that function.

like image 38
Joseph Lisee Avatar answered Oct 05 '22 04:10

Joseph Lisee