Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Form, Getting FPS?

I'm writing a windows application which is displaying real time data on a map. Is there a simple way to get the FPS (Frames Per Second)?

Thanks, couldn't find much on google. This is C#, .NET 4.0.

like image 788
dave2118 Avatar asked May 18 '26 23:05

dave2118


1 Answers

Calculating FPS may be something as simple as this (if precision is not of uttermost importance):

DateTime _lastCheckTime = DateTime.Now;
long _frameCount = 0;

// called whenever a map is updated
void OnMapUpdated()
{
    Interlocked.Increment(ref _frameCount);
}

// called every once in a while
double GetFps()
{
    double secondsElapsed = (DateTime.Now - _lastCheckTime).TotalSeconds;
    long count = Interlocked.Exchange(ref _frameCount, 0);
    double fps = count / secondsElapsed;
    _lastCheckTime = DateTime.Now;
    return fps;
}

Set an update timer to call GetFps() every second to get the value. Note that there should be no concurrent calls to this method, since every call resets counters and start time.

like image 69
Groo Avatar answered May 21 '26 16:05

Groo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!