Skip to main content
gamedev.net
🔒 Locked

Function timing

Started by VanillaSnake21 Jan 18, 2008 at 3:08 AM 4 replies 850+ views
Original Post
VanillaSnake21
VanillaSnake21
Hi, I would like to know the best way to measure the time it takes for the function to run. I want to test out different line drawing algorithms, and see how they compare. I read somewhere that you can use regular windows time functions, but won't the extra function calls interfere with timing? BTW I am using VC6 which does come with a profiler, but I don't think it's accurate. I rerun the same code 5 or 6 times and it's output varies from .24s to .6s. If I'm trying to improve algorithm speed won't every millisecond matter? Thanks.
You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts
adam4813
adam4813
Read up on QueryPerformanceCounter.
Structural
Structural
Quote:
Original post by VanillaSnake21
Hi, I would like to know the best way to measure the time it takes for the function to run. I want to test out different line drawing algorithms, and see how they compare. I read somewhere that you can use regular windows time functions, but won't the extra function calls interfere with timing? BTW I am using VC6 which does come with a profiler, but I don't think it's accurate. I rerun the same code 5 or 6 times and it's output varies from .24s to .6s. If I'm trying to improve algorithm speed won't every millisecond matter? Thanks.


Any kind of timing measurement will influence the execution speed of your app. If you want to minimize the effect of the calls make sure both algorithms call the timing functions the same number of times.

Windows' performance counter will do the trick. Standard time functions do not have a high enough resolution to do proper profiling.
Take a look at http://support.microsoft.com/kb/172338
STOP THE PLANET!! I WANT TO GET OFF!!
Spodi
Spodi
If possible, you can also loop over the code multiple times. This helps compensate for the resolution, along with can help give you a more average value.
NetGore - Open source multiplayer RPG engine
Antheus
Antheus
Pseudo algorithm works something like this:
desired_time = 2500; // let's use millisecondsn_runs = 1;time_taken = 0;do {  time_start = // get current time  for [1 .. n_runs] {    // run whatever you're profiling  }  time_end = // get_current_time;  time_taken = time_end - time_start;  n_runs = n_runs * 2;} while (time_taken < desired_time);average_time = (n_runs/2)/time_taken;


The only parameter here is the desired time to run. Then, the number of runs to achieve that will be dynamically determined. This gives you the convenience of making the code usable for algorithms with variable running times.

Ideally, you'd repeat this whole measurement several times, then choose the best samples, and use average of that.

With such approach, accuracy of timer itself isn't a problem, just set running time to some reasonably large factor of timer resolution. For low resolution timers, you may need to let it run for several seconds.

VanillaSnake21
VanillaSnake21
Thanks guys [smile]
You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.