title: Abysmal performance with vectors in c++
body: <p><strong>Disclaimer:</strong> i just started programming in c++.</p>
  
  <hr>
  
  <p>So i made a little program in c++ using SFML where i have 300 particles stored in a vector that are attracted to each other.</p>
  
  <p>The program runs at 17 FPS!! i tried doing the same using a standard array and it didn't run much faster.</p>
  
  <p>The same code in java using the Array libgdx class runs at 5000 FPS.
  Using the java.util.Vector class runs at 2000 FPS.</p>
  
  <p>Debugging i found out that most of the delay comes from retrieving an element from the vector.</p>
  
  <p>I know i'm doing something wrong in that regard, so i would be grateful if someone can help me out.</p>
  
  <p>Here is the update loop.</p>
  
  <pre><code>void update(RenderWindow&amp; window) {
      window.clear();
  
      for (size_t i = 0, size = particles.size(); i != size; ++i) {
          Particle &amp;part = particles[i];
  
          glm::vec2 forceSum;
  
          for (size_t j = 0, size = particles.size(); j != size; ++j) {
              Particle &amp;part2 = particles[j];
              if (j == i) 
                  continue;
  
              glm::vec2 v1 = part.getPos() - part2.getPos();
              glm::vec2 v2 = glm::normalize(v1);
  
              if (glm::length(v1) &lt; 50)
                  forceSum += (v2 * -0.1f);
          }
  
          part.applyForce(forceSum);
          part.update();
  
          shape.setPosition(part.getPos().x, part.getPos().y);
          window.draw(shape);
      }
  
      window.display();
  }
  </code></pre>
  
  <p>I can show more code if requested.</p>
  
  <p>Thanks in advance!</p>
  
  <hr>
  
  <p>main.cpp</p>
  
  <pre><code>#include "stdafx.h"
  
  
  using namespace std;
  using namespace sf;
  
  void update(RenderWindow&amp; window);
  
  int t = 0;
  CircleShape shape(4);
  
  vector&lt;Particle&gt; particles;
  
  int main() {
      ContextSettings settings;
      settings.antialiasingLevel = 16;
      RenderWindow window(VideoMode(800, 800), "test", Style::Default, settings);
  
      shape.setFillColor(Color(155, 155, 155, 124));
      srand(4);
  
      for (int i = 0; i &lt; 300; i++) {
          Particle particle = Particle(glm::vec2(rand() % 800, rand() % 800), rand() % 10);
          particles.push_back(particle);
      }
  
      while (window.isOpen())
      {
          Event event;
          while (window.pollEvent(event))
          {
              if (event.type == Event::Closed)
                  window.close();
          }
  
          update(window);
      }
      return 0;
  }
  
  void update(RenderWindow&amp; window) {
      window.clear();
  
      for (size_t i = 0, size = particles.size(); i != size; ++i) {
          Particle &amp;part = particles[i];
  
          glm::vec2 forceSum;
  
          for (size_t j = 0, size = particles.size(); j != size; ++j) {
              Particle &amp;part2 = particles[j];
              if (j == i) 
                  continue;
  
              glm::vec2 v1 = part.getPos() - part2.getPos();
              glm::vec2 v2 = glm::normalize(v1);
  
              if (glm::length(v1) &lt; 50)
                  forceSum += (v2 * -0.1f);
          }
  
          part.applyForce(forceSum);
          part.update();
  
          shape.setPosition(part.getPos().x, part.getPos().y);
          window.draw(shape);
      }
  
      window.display();
  }
  </code></pre>
  
  <p>Particle.h</p>
  
  <pre><code>#pragma once
  
  #include &lt;SFML/Graphics.hpp&gt;
  
  class Particle {
  public:
      Particle(glm::vec2 pos, float m);
      void update();
      void applyForce(const glm::vec2 &amp;fce);
      void setPos(const glm::vec2 &amp;pos);
      void setV(const glm::vec2 &amp;vel);
      const glm::vec2 getPos();
      const glm::vec2 getV();
  private:
      float mass;
      glm::vec2 acceleration = glm::vec2(0, 0);
      glm::vec2 position = glm::vec2(0, 0);
      glm::vec2 velocity = glm::vec2(0, 0);
  };
  </code></pre>
  
  <p>Particle.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include "Particle.h"
  #include &lt;iostream&gt;
  
  using glm::vec2;
  
  Particle::Particle(vec2 pos, float m) {
      mass = m;
      position = pos;
  }
  
  void Particle::update() {
      velocity += acceleration;
      if (position.x + velocity.x &gt; 800 || position.x + velocity.x &lt; 0 ||     position.y + velocity.y &gt; 800 || position.y + velocity.y &lt; 0) {
          velocity.x = 0; velocity.y = 0;
      }
      position += velocity;
  }
  
  void Particle::applyForce(const vec2 &amp;fce) {
      acceleration = fce/mass;
  }
  
  void Particle::setPos(const vec2 &amp;pos) {
      position = pos;
  }
  
  void Particle::setV(const vec2 &amp;vel) {
      velocity = vel;
  }
  
  const vec2 Particle::getV() {
      return velocity;
  }
  
  
  const vec2 Particle::getPos() {
      return position;
  }
  </code></pre>