In the following code, how to measure total elapsed time of function1()
and function3()
?
In the real situation, function1()
and function3()
are scattered everywhere in a complex program. I need something that can use the name of the function to start and stop a timer
so in the end I can print out how long each function takes in total.
Please note that the time elasped of function1()
is not the needed value. The total time elasped of function1()
which is called everywhere many times is the needed value.
int function1()
{
int i = 1;
i++;
return i;
}
int function2()
{
int i = 1;
i++;
return i;
}
int function3()
{
int i = 1;
i++;
return i;
}
int main(int argc, char *argv[])
{
size_t t = 0;
while (t < 1000000)
{
// I want something like startTimer(function1)
function1();
// I want something like pauseTimer(function1)
function2();
// I want something like startTimer(function3)
function3();
// I want something like pauseTimer(function3)
}
// printTimer() here will print the total elapsed time of function 1 and
//function 3
getchar();
return 0;
}