DateTime.Now Refresh Interval too granular!?

  • Thread starter Thread starter Gareth
  • Start date Start date
G

Gareth

I am trying to use DateTime.Now to apply a frame limit to my 3d
application. Below is the code involved. In this example I have hard
coded it for 60 frames per second (1000 milliseconds / 60 fps).

public void Execute()
{
while(!_close)
{
DateTime n=DateTime.Now;
TimeSpan t=n.Subtract(_lastRender);

if(t.TotalMilliseconds>=1000.0d/60.0d)
{
_lastRender=n;
_render();
}
}

}

The problem is I always get 33 frames per second. If I remove the
frame limiter I get 2000+ frames per second. If I log the value of
t.TotalMilliseconds to a text file every iteration I get the
following:

Notice that TotalMilliseconds doesn't change at all for several
iterations, then suddenly doubles...

20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 31.2558
20/09/2007 09:50:11: 0
20/09/2007 09:50:11: 0
20/09/2007 09:50:11: 0
20/09/2007 09:50:11: 0
 
Gareth said:
I am trying to use DateTime.Now to apply a frame limit to my 3d
application. Below is the code involved. In this example I have hard
coded it for 60 frames per second (1000 milliseconds / 60 fps).

public void Execute()
{
while(!_close)
{
DateTime n=DateTime.Now;
TimeSpan t=n.Subtract(_lastRender);

if(t.TotalMilliseconds>=1000.0d/60.0d)
{
_lastRender=n;
_render();
}
}

}

The problem is I always get 33 frames per second. If I remove the
frame limiter I get 2000+ frames per second. If I log the value of
t.TotalMilliseconds to a text file every iteration I get the
following:

Notice that TotalMilliseconds doesn't change at all for several
iterations, then suddenly doubles...

This is because DateTime uses the systems RTC, which is incremented every 10
or 15 milliseconds depending on the system HW. When you need finer
granularity you'll have to use the System.Diagnostics.Stopwatch class which
wraps the high-perf Win32 API's.

Willy.
 
Thanks, couldn't use StopWatch because I am on v1.1 of the framework
but that lead me to the answer - using QueryPerformanceCounter /
Frequency.

A nice smooth 60fps is coming from my graphics engine now :D

G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top