Timing functions in Visual C++

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0

Does anyone know of a quick and easy way to find out the time it takes to compute a function or perform some kind of process in Visual C++?

Right now, I'm trying to use the time functions in "time.h" but their resolution is very poor. It seems like it can't measure anything that takes less than 1 second (it reports that 0 seconds have occurred). Of course, most computers are so fast that it takes less than one second to perform an action. Is there any other timing functions that have a finer resolution (like in the miliseconds)?

************************************
// Start timer
start = time(NULL);

myFunction();

// Get ending time
end = time(NULL);

cout << "The function took about " << difftime(end, start) << " seconds to perform.\n\n";
 

I usually use GetTickCount() which returns milliseconds

long start = ::GetTickCount();
// do stuff
long stop = ::GetTickCount();

print("Elapsed time = %1.1f", float(stop - start / 1000f);

Some Windows guru told me it is not 100% acurate and pointed me to some really complex API call. GetTickCount() works fine for me.

 

singh

Golden Member
Jul 5, 2001
1,449
0
0
GetTickCount() does NOT have a high enough resolution. I think the max resolution is around 15ms on NT.
 

squirtle24

Senior member
Apr 17, 2001
253
0
0
You can use the Multimedia Timer functions for greater accuracy(ms). Search for timeGetTime in the help files.

If you're timing functions, you might also try profiling.