Skip to main content
gamedev.net
๐Ÿ”’ Locked

testing code performance

Started by sj Oct 5, 2002 at 3:29 AM 2 replies 300+ views
Original Post
sj
sj
Is there a way to test how long it takes for a code to execute? I''ve tried using timeGetTime and QueryPerformanceCounter but they don''t seem to work very well.
remi
remi
quote:
I've tried using timeGetTime and QueryPerformanceCounter but they don't seem to work very well.


why can u say that? Do something like this:

DWORD dwElapsedTime=timeGetTime();//Initialize ur variable
MyFunction(); //call ur function
dwElapsedTime-=timeGetTime();//get the interval of time elapsed

Hope it Helps

[edited by - remi on October 5, 2002 4:58:00 AM]
"...and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces."----------Scott Meyers, "Effective C++"
botman
botman
Or try something like this...

  #include <windows.h>
#include <stdio.h>

void main(void)
{
long x, y;
LARGE_INTEGER PerfFrequency;
LARGE_INTEGER PerfCountStart, PerfCountEnd;
LARGE_INTEGER PerfDiff;
float diff_time;

QueryPerformanceFrequency(&PerfFrequency);

if (PerfFrequency.QuadPart == 0)
{
printf("Your system doesn't support high-resolution timers!\n");
return;
}

printf("ticks per second = %12I64u\n", PerfFrequency);

PerfFrequency.QuadPart /= 1000; // convert to ticks per millisecond


QueryPerformanceCounter(&PerfCountStart);

y = 0;
for (x=0; x < 100000000L; x++)
y = y + x; // don't optimize the loop away


QueryPerformanceCounter(&PerfCountEnd);
PerfDiff.QuadPart = PerfCountEnd.QuadPart - PerfCountStart.QuadPart;

diff_time = (float)PerfDiff.QuadPart / (float)PerfFrequency.QuadPart;

printf("PerfDiff = %12I64u diff_time = %12.5f milliseconds\n", PerfDiff, diff_time);
}

botman

[edited by - botman on October 5, 2002 3:25:29 PM]

Topic Locked

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

Sign in to reply to this topic.