C ++: track time function

Keeping track of how many times a function is called is easy to carry the counter as an argument to the function. This is also easy when returning one of the called function. But I do not want to go this way. The reason for this is that it seems like poor programming (letting the function know too much information). Is there a better way to track how many times this function has been called?

I'm just looking for concepts that I could learn. Providing code examples is optional, but may be helpful.

Edit: I'm not looking for profiling tools. Let me add some code to understand my point. Since the scope of funcCounter ends mostly, I have no way to return the variable myFunction, which will increment funcCounter. I could return 1 from myFunction and then increase the funcCounter this way, but that doesn't seem like very good programming. Is there any other way to do this?

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;
}
+3
source share
6 answers

Have a static variable in your function and keep incrementing it every time you call the function.

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

, gcov, . ( , Microsoft Microsoft Visual ++)

+15

, gcov ( Linux). , , , , .

+4

, . , (ab) .

, , .

+3

, static , accessor / reset:

class X
{
private:
    /* diagnostics */
    static int counter = 0;
    int read_counter() const { return counter; }
    void reset_counter() { counter = 0; }

public:
    /* real code */
    fcn() {
        ++counter;
        /* ... */
    }
};

, .

, , , .

+3

, , ( ), f() .

Note. There gettimeofday()is some overhead for that, so you can use a different synchronization method, but this is a completely different topic, worthy of his own question (and was discussed earlier on SO).

#include <iostream>
#include <string>
#include <map>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <sys/time.h>

class PerfStats 
{
private:
    std::string which_;
    timeval begin_;

public:

    PerfStats(std::string const &file, int line)
    {
        std::stringstream ss;
        ss << file << ':' << line;
        which_ = ss.str();
        gettimeofday(&begin_, NULL);
    }

    ~PerfStats()
    {
        timeval end;

        gettimeofday(&end, NULL);

        Times[which_] = (end.tv_sec - begin_.tv_sec) + (end.tv_usec - begin_.tv_usec)/1000000.0;
        ++Counts[which_];
    }

    static std::map<std::string, double> Times;
    static std::map<std::string, unsigned int> Counts;

    static void Print()
    {
        for(std::map<std::string, double>::iterator it = Times.begin(); it != Times.end(); ++it)
            std::cout << it->first << " :\t" << it->second << "s" << std::endl;

        for(std::map<std::string, unsigned int>::iterator it = Counts.begin(); it != Counts.end(); ++it)
            std::cout << it->first << " :\t" << it->second << " times" << std::endl;
    }
};


std::map<std::string, double> PerfStats::Times;
std::map<std::string, unsigned int> PerfStats::Counts;

void f()
{
    PerfStats(__FILE__, __LINE__);
    usleep(1);
}

main()
{
    srand(time(NULL));

    for(int i = 0; i < rand(); ++i)
        f();
    PerfStats::Print();
}

Output Example:

test.cpp:54 :   2e-06s
test.cpp:54 :   21639 times
+3
source

A bad coding style, but perhaps adding global variables and, if necessary, locking the mutex can do the trick.

+2
source

Source: https://habr.com/ru/post/1750019/


All Articles