Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Measure FPS  (Read 4297 times)

0 Members and 1 Guest are viewing this topic.

chilliboy999

  • Newbie
  • *
  • Posts: 16
    • View Profile
Measure FPS
« on: December 23, 2013, 02:23:46 pm »
Hey guys,
i thought of a way to measure fps.
So, i came up with the following idea:

C# Code:
        private void Render()
        {
            while (renderWin.IsOpen())
            {
                MeasureFPS();
                renderWin.Clear();
                renderWin.DispatchEvents();
                renderWin.Draw(spPlayer);
                renderWin.Draw(txtFps);
                renderWin.Display();
            }
        }
        private void MeasureFPS()
        {
            fps++;
            if (DateTime.Now.Subtract(lastTimeMeasured).Seconds > 1)
            {
                txtFps.DisplayedString = "FPS: " + fps.ToString();
                lastTimeMeasured = DateTime.Now;
                fps = 0;
            }
        }
 

Is this a valid way to determine my fps?
Cause i have about 4000 per second.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Measure FPS
« Reply #1 on: December 23, 2013, 02:37:57 pm »
Yes, this as a valid way but it isn't 100% accureate. Because DateTime.Now.Subtract(lastTimeMeasured) could be around 1.01 seconds, but you can ignore that. The difference is smaller than 1.

Edit: you should write
if (DateTime.Now.Subtract(lastTimeMeasured).Seconds >= 1.F)

AlexAUT
« Last Edit: December 23, 2013, 03:19:42 pm by AlexAUT »

chilliboy999

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Measure FPS
« Reply #2 on: December 23, 2013, 02:54:00 pm »
Ah ok thanks ;)
And it is like you say
>= 1
cause
> 1
equals 2 seconds and more;)
I didnt see that at the first time

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Measure FPS
« Reply #3 on: December 23, 2013, 03:25:02 pm »
I found in the microsoft documentation that the DateTime is inaccurate, high resolution. So if you want to measure the FPS accurate you should use a stopwatch http://msdn.microsoft.com/en-us//library/system.diagnostics.stopwatch(v=vs.110).aspx



AlexAUT

chilliboy999

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Measure FPS
« Reply #4 on: December 23, 2013, 04:19:48 pm »
Thank you for the hint :)
Now i'm using stopwatch.

Btw. Schöne Grüße nach Österreich ;)

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Measure FPS
« Reply #5 on: December 23, 2013, 04:29:21 pm »
Btw. Schöne Grüße nach Österreich ;)

Kein Problem, danke und frohe Weihnachten!  :)


AlexAUT

chilliboy999

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Measure FPS
« Reply #6 on: December 23, 2013, 04:30:37 pm »
Kein Problem, danke und frohe Weihnachten!  :)

Danke ;)
Die wünsch ich dir auch :)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Measure FPS
« Reply #7 on: December 23, 2013, 05:43:07 pm »
If you are looking for SFML style sf::Clock and sf::Time classes I have a nice implementation of them in my NetEXT library (see my signature).
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

chilliboy999

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Measure FPS
« Reply #8 on: December 23, 2013, 09:35:59 pm »
Thanks zsbzsb for your suggestion :)
hmm... so many possibilities.
It's difficult to take a decision ;)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Measure FPS
« Reply #9 on: December 23, 2013, 10:41:48 pm »
Well maybe this will help you make a decision. Internally NetEXT Clock/Time classes use a Stopwatch, however if you want a high resolution timer you must use Stopwatch.ElapsedTicks (and then you need to deal with conversions). NetEXT Clock/Time internally use ticks to provide high resolution time measurement. Including overridden operators to easily add/subtract/multiply time.

It is much cleaner IMO to use NetEXT and get high resolution timer than trying to do all the conversions directly from Stopwatch in your own code. (not to mention you can then directly convert most SFML code that uses sf::Clock and sf::Time)  ;)
« Last Edit: December 23, 2013, 10:43:49 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

chilliboy999

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Measure FPS
« Reply #10 on: December 26, 2013, 02:34:39 pm »
It sounds like that NetEXT would be the most simple and accurate possibility ;)