Sign in to follow this  

I want to set up a timer to calculate how long a function runs....

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my continuing study of algo's I am now trying to figure out how to setup a simple timer with in a function so that I can see how long that function runs for and later display it back to the user. Any suggestions?

Share this post


Link to post
Share on other sites
Get the time before and after the function is called using timeGetTime() or GetTickCount() (pick your favorite). Be sure to get, translate and dispatch messages before you get the time.
Take the time before and subtract that from the time after and that will give you the time in milisecs that the function took to execute. Outputting will depend on what type if interface you are using.
The function may execute under one milisecond so you may have to execute multiple times (like 1000) and divide the time by that number.

Share this post


Link to post
Share on other sites
QueryPerformanceCounter & QueryPerformanceFrequency offer a better resolution than timegettime & GetTickCount(). The latter functions frequently offer a resolution of only +/- 10ms.

here's a little tut on how to use them:
http://www.decompile.com/cpp/faq/windows_timer_api.htm

Word of caution: timers of any kind are unreliable on multi-processor machines (and maybe multi-core machines, not sure about this parenthetical). you should bind the thread using the timer to a specific CPU.

-me

Share this post


Link to post
Share on other sites
Instead of using high resolution timers to get microseconds, I would advise to use GetTickCount() (resolution of 2ms) instead and simply build another loop around the algorithm loop and take the average times.
This gives you a more numerical stable result, since individual times can differ a lot not only depending on the algorithm.

Share this post


Link to post
Share on other sites
Quote:
Original post by kiome
Instead of using high resolution timers to get microseconds, I would advise to use GetTickCount() (resolution of 2ms) instead and simply build another loop around the algorithm loop and take the average times.
This gives you a more numerical stable result, since individual times can differ a lot not only depending on the algorithm.


I guess it all boils down to what the function is doing. 2ms is an eternity (it's 1/8 of your total frame time running at 60FPS). So a resolution of 2ms is fine for your entire frame timing, but if you're trying to get stats on a low level function then something with a resolution of 2ms isn't going to be worth much.

I mean, my entire object partitioning system (applying objects each frame, quering radial areas to get objects in that zone) is running at less than a millisecond per frame and i'm still trying to optimize it. For something like that, you definitely need the higher resolution timers (assuming your going the route of writing your own profiling tool).

Either way, you definitely want to do an average over many frames, but taking an average doesn't make the resolution of your timer any better.

Like i said, the choice of timer depends on the complexity of the function you're trying to time.

For optimizing your program you're better off using a package like VTune than by splattering your own timer stats everywhere.

-me

Share this post


Link to post
Share on other sites
I agree, 2 milliseconds is a lot of time for a CPU.
If you want to measure durations of parts of your code while actually running the whole application (because there might be different factors involved that have effect on the timing) you will probably need to apply microsecond resolution timers.

I was more thinking about set algorithms/functions that you want to test individually.
For this case I build testing functions that embed those said functions in a double loop (one that increases its duration to make it measurable, the other to decrease random effects caused by CPU/OS load, thread switching ect.).
This type of testing only makes sense when you have different variations of the function and you want to see how they compete with each other.

I guess it comes down to what type of function you want to test.

Share this post


Link to post
Share on other sites
Hmmm...this also gets me wonder about extremely lengthy functions calls. For example lest consider an algo that calculates all possible premutations of 10 values. That would come out to 3.6 million diffrent combinations of those 10 numbers. Needless to say this is not on the milisecond level. We are talking about 45 mins or so on an average computer. Which this leads us with a number in the millions. Is there a better way to calculate long running functions or do the same princiable apply.

Share this post


Link to post
Share on other sites
The same principle applies, except that I would not do double loops in this case, I think a function that takes 45 minutes to finish is numerically stable enough ;).

If you fear data type overflow, you have to know that GetTickCount() gives you the uptime (time passed since windows boot) in milliseconds, that means you can run the program about 24 days till it returns you a negative value (long int goes up to +2^31).

And even if you for example run a server software that should (try) to run so long and even longer, you can introduce measures to check for overflow by adding a counter that would simply safe the number of occured overflows.
It would only affect the difference of the values stopped "around" the overflow (one before and the other in the negatives).

Share this post


Link to post
Share on other sites

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this