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

Author Topic: Creating a new sf::Event object in each loop?  (Read 4285 times)

0 Members and 1 Guest are viewing this topic.

phuongtam

  • Newbie
  • *
  • Posts: 1
    • View Profile
Creating a new sf::Event object in each loop?
« on: September 30, 2017, 06:07:45 pm »
Hello, everyone! I have a question about most SFML code I see. Why are they redeclaring an sf::Event object after while(Window.IsOpened()). Wouldn't this cause the sf::Event constructor to be called each time creating a new object and hurting performance slightly? Is there any difference in declaring sf::Event before the main window loop?
« Last Edit: October 13, 2017, 12:51:07 am by binary1248 »

Eyfenna

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Creating a new sf::Event object in each loop?
« Reply #1 on: September 30, 2017, 07:36:27 pm »
Did wonder about this myself.
personally I tend to declare sf::Event before the main loop.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Creating a new sf::Event object in each loop?
« Reply #2 on: September 30, 2017, 08:06:27 pm »
sf::Event is a POD there's no constructor and this can easily be optimized by the compiler.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Creating a new sf::Event object in each loop?
« Reply #3 on: October 01, 2017, 02:11:18 pm »
It's also a good example of keeping variables in as local a scope as possible. There is no use to having an sf::Event outside of the window being open so it makes no sense to have one outside of the window.isOpen() loop.

As mentioned, there should not be any performance or noticable performance reduction from creating an sf::Event once per cycle.

However, if you feel that you absolutely must keep from the event being declared every cycle, you can have the best of both worlds (correct scope and persistent life) by simply declaring it as static.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Creating a new sf::Event object in each loop?
« Reply #4 on: October 01, 2017, 03:28:32 pm »
However, if you feel that you absolutely must keep from the event being declared every cycle, you can have the best of both worlds (correct scope and persistent life) by simply declaring it as static.

But static will/may be the slowest, because of cache locality ;).

As eXpl0it3r said, sf::Event is POD, so creating and instance is done by increasing the stack pointer register by some amount (size of sf::Event (+ padding/alignment)). This usally is done in one CPU-cycle or less. So placing it inside the loop is the best option, because as hapax said, keep variables as local as possible.



AlexAUT

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Creating a new sf::Event object in each loop?
« Reply #5 on: October 03, 2017, 11:00:42 pm »
Totally agree with others: keep variables as local as possible.

This is a typical example of premature optimization. Object creation in C++ is not per se a costly operation, even in the presence of user-defined constructors. What really happens behind the scenes largely depends on the context. Even in languages where there is overhead (most mainstream languages have a GC and objects are created through dynamic allocation), people don't refrain from using objects because they fear the performance hit ;)

Only start worrying about such micro-optimizations once you can prove they matter.

It would of course be a different story if the object being created were more heavy-weight (e.g. of a resource class that performs loading).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: