Note that there are some explanatory texts on larger screens.

plurals
  1. POC++11: Why does this range loop decrease FPS by 35?
    primarykey
    data
    text
    <p>I'm writing a game using SFML and C++11 features, such as the range loop. When working on tile maps, I basically made a class for each map tile, a light-weight class that simply contains its sprite, position, and such, and then built some nested vectors to represent the game map layers.</p> <p>In order to optimize the process of drawing thousands of objects on the screen at a time, I was simply drawing what the player sees. This went well.</p> <p>I have the following method that renders the game map, the condition basically returns true if the tile position is within the camera boundaries</p> <pre><code>void gameMap::render(sf::RenderWindow &amp;winMain, sf::Vector2f offset) { for(vector&lt;int&gt; vec1 : backgroundData) for(int i : vec1) if(collides(i.pos, offset) myWindow.draw(i.sprite); } </code></pre> <p>it works fine, however in-game I am getting 30 FPS roughly, and a lot of rough movement. But what surprises me is that the code bellow does the same thing, renders the same amount of tile sprites, but <strong>runs at 65 fps</strong> and the movement is perfectly smooth</p> <pre><code>void gameMap::render(sf::RenderWindow &amp;winMain, sf::Vector2f offset) { for(int i = 0; i &lt; backgroundTiles.size(); i++) for(int j = 0; j &lt; backgroundTiles[i].size(); j++) if(collides(backgroundTiles[i][j].pos, offset) myWindow.draw(backgroundTiles[i][j].sprite); } </code></pre> <p>Why is this happening? Is the C++11 range-based loop so much slower than the old school for? I really want to hear an answer to this, because my eyes honestly prefer the range based loop, and I'd hate to find out that the range based loop is twice as slow.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload