3

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.

dave2118
  • 206
  • 6
  • 22
  • what type of data you are using to show on map? could you please explain it further. – Ovais Khatri Jul 18 '11 at 19:12
  • I'm using a purchased mapping control through a third party. We're displaying realtime power consumption throughout the country at 30 times a second. – dave2118 Jul 18 '11 at 19:14

2 Answers2

4

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.

vgru
  • 49,838
  • 16
  • 120
  • 201
  • Thanks! I don't need it to specific, but some kind of benchmark. – dave2118 Jul 18 '11 at 19:43
  • @dave2118: it was a pretty quick and thread-unsafe example. If different threads these methods at once, you should use the `Interlocked` class to ensure thread safety (I've updated the example). – vgru Jul 18 '11 at 19:59
0

How are you drawing your map? It all depends on what technology you are using... My recomendations go for managed DirectX if you are doing graphic intensive work.

Try to get more info here: http://www.programmersheaven.com/2/time-fps

Denis Biondic
  • 7,943
  • 5
  • 48
  • 79