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

Author Topic: FPS are falling down  (Read 2719 times)

0 Members and 1 Guest are viewing this topic.

Platyna

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
FPS are falling down
« on: June 25, 2012, 04:09:09 am »
Hello,

I have following code:
#include <SFML/Graphics.hpp>
#include <cstdio>
#include <SFML/System.hpp>
#include <queue>

int getFPS() {
    static sf::Clock clock;
    static int frames = 0;
    static int updateTime = 1000000;
    static double fps = 0;
    frames++;

    if (clock.getElapsedTime().asMicroseconds() > updateTime) {
        fps = frames;
        frames = 0;
        clock.restart();
    }

    return fps;
}

int main() {

    sf::RenderWindow* renderTarget = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Trololo", sf::Style::Close);

    char str[50] = "0";

    while (true) {
        renderTarget->clear();

        sprintf(str, "%d\n", getFPS());
        sf::String string(str);
        sf::Text fpsText(string);
        fpsText.setColor(sf::Color(0, 0, 255));
        renderTarget->draw(fpsText);

        renderTarget->display();
    }

    return 0;
}

 

And it starts with 2100 fps. But after 20-30 minutes it's only 1100 fps.
I have no idea what is the reason for it.
« Last Edit: June 25, 2012, 04:12:22 am by Platyna »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: FPS are falling down
« Reply #1 on: June 25, 2012, 09:00:26 am »
SFML wasn't build for extremly high FPS. Also in that region each frame needs to be rendered 470us, which is insanely fast any small changes will influence the FPS dramatically. I think it's not that important how high your FPS can get, because you normally don't want your applications to run with such a high FPS - maxing out your CPU for no reason - and if you write an application which needs that highperformance then you'll limit your customer rapidly - not all have such a good CPU/GPU.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: FPS are falling down
« Reply #2 on: June 25, 2012, 10:44:52 am »
try to pop events from stack

while (true) {
        sf::Event event;
        while (renderTarget.pollEvent(event)) {
        }
        renderTarget->clear();

        sprintf(str, "%d\n", getFPS());
        sf::String string(str);
        sf::Text fpsText(string);
        fpsText.setColor(sf::Color(0, 0, 255));
        renderTarget->draw(fpsText);

        renderTarget->display();
    }
 
« Last Edit: June 25, 2012, 10:46:54 am by Acrobat »

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: FPS are falling down
« Reply #3 on: June 26, 2012, 07:30:22 am »
    static int frames = 0;
    static int updateTime = 1000000;
    static double fps = 0;
 

Why are you using int and double?

...(sf::VideoMode(800, 600, 32), "Trololo"

R.I.P. Trololo  :'(

sf::RenderWindow* renderTarget = new sf::RenderWindow

If you create something with new, you must delete it

And it starts with 2100 fps. But after 20-30 minutes it's only 1100 fps.

Maybe some other program using OpenGL?
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Platyna

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: FPS are falling down
« Reply #4 on: June 26, 2012, 04:05:04 pm »
    static int frames = 0;
    static int updateTime = 1000000;
    static double fps = 0;
 
Why are you using int and double?
Oh, I used other algorithm earlier and I forgot change this.


sf::RenderWindow* renderTarget = new sf::RenderWindow

If you create something with new, you must delete it
It's not required if object that I create with new is used to the end. It isn't memory leak. All allocated memory is freed automatically when application is closed.
If I lose last pointer to the object then it's memory leak.


And it starts with 2100 fps. But after 20-30 minutes it's only 1100 fps.

Maybe some other program using OpenGL?
Only NetBeans is running.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: FPS are falling down
« Reply #5 on: June 27, 2012, 12:10:30 pm »
sf::String and sf::Text contain STL containers which make use of the standard new allocator. If you declare those variables in the scope of the while loop you are constantly allocating and freeing memory every frame. Depending on how the OS allocator works this can be stable even after a long time or it can miserably fail due to memory fragmentation which is likely in your case. This is the main reason why all those big name games often make use of memory pools. The standard allocators were not made for such aggressive allocation schemes like is needed in many applications nowadays.

If you were to move the declaration of those 2 variables outside of the while loop you would limit the execution of the loop body to only making use of the stack (ignoring the clock.getElapsedTime().asMicroseconds() because there is no way to avoid that). That should theoretically increase the short-term and long-term performance.

Quote
It's not required if object that I create with new is used to the end. It isn't memory leak. All allocated memory is freed automatically when application is closed.
If I lose last pointer to the object then it's memory leak.
Turn off the virtual memory manager of your operating system and eventually it will leak to death. It is true that this practice is rooted back in the days of real-mode operating systems, however it is still considered good programming practice to free everything anyways. It is technically considered a leak even if the operating system frees it at the end of execution and any leak tracer like valgrind will report it as such.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: FPS are falling down
« Reply #6 on: June 28, 2012, 11:59:43 pm »
It's not required if object that I create with new is used to the end. It isn't memory leak. All allocated memory is freed automatically when application is closed.
This is wrong.

The C++ standard isn't required to deallocate the memory upon program shutdown. Although modern operating systems usually do this for memory, this doesn't apply automatically to other resources. Don't forget that delete invokes destructors of class types, which often contain further clean-up code. Maybe your application allocates other resources (e.g. network connections or anything) that need to be closed correctly.

Anyway, using new and delete should generally be avoided in end-user code. There are almost no situations where manual memory management is appropriate and worth the trouble it brings. Here, it is completely unnecessary, as you can just allocate the sf::RenderWindow on the stack:
sf::RenderWindow renderTarget(sf::VideoMode(800, 600, 32), "Trololo", sf::Style::Close);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything