I explained in the forums an easy method for determining the speed of a C/C++ function. Just include the following function into your project file:
long getExecutionTime(void (*func)()); // define Function
long GetExecutionTime(void (*func)()){
long cTick, nTick;
cTick = GetTickCount();
(*func)();
nTick = GetTickCount();
return nTick - cTick;
}
Then use the function by passing in the function name into GetExecutionTime, for example:
void doSleep(){
Sleep(5000);
}
int main(int argc, char *argv[])
{
cout << "Code executed in: " << GetExecutionTime(doSleep) << "ms \n";
system("PAUSE");
return EXIT_SUCCESS;
}
And your done! This is a great way to benchmark the speed of your functions and to figure out problematic places where there might be unnecessary slow downs.

Home
Projects
Hosting
Advertising
About
Archive
Contact
Forums
Subscribe
You can make this more generic and useful by using the constructor and destructor methods (see also the RAII). Scoping then allows you to time functions with a varying number of parameters or more complicated constructs. E.g.,
struct ExecutionTimer
{
ExecutionTimer(long & result)
: result_(result)
{
result_ = – GetTickCount();
}
~ExecutionTimer()
{
result_ += GetTickCount();
}
long & result_;
};
int main()
{
long timer = 0;
{
ExecutionTimer et(timer);
doSleep();
}
std::cout << “Code executed in ” << timer << “ms \n”;
}
Reply