title: How is SFML so fast?
body: <p>I need to draw some graphics in c++, pixel by pixel on a window. In order to do this I create a SFML window, sprite and texture. I draw my desired graphics to a uint8_t array and then update the texture and sprite with it. This process takes about <strong>2500 us</strong>. Drawing two triangles which fill the entire window takes only <strong>10 us</strong>. How is this massive difference possible? I've tried multithreading the pixel-by-pixel drawing, but the difference of two orders of magnitude remains. I've also tried drawing the pixels using a point-map, with no improvement. I understand that SFML uses some GPU-acceleration in the background, but simply looping and assigning the values to the pixel array already takes hundreds of microseconds.</p>
  <p>Does anyone know of a more effective way to assign the values of pixels in a window?</p>
  <p>Here is an example of the code I'm using to compare the speed of triangle and pixel-by-pixel drawing:</p>
  <pre><code>#include &lt;SFML/Graphics.hpp&gt;
  #include &lt;chrono&gt;
  using namespace std::chrono;
  #include &lt;iostream&gt;
  #include&lt;cmath&gt;
  
  uint8_t* pixels;
  
  int main(int, char const**)
  {
      const unsigned int width=1200;
      const unsigned int height=1200;
      sf::RenderWindow window(sf::VideoMode(width, height), &quot;MA: Rasterization Test&quot;);
      pixels = new uint8_t[width*height*4];
      sf::Texture pixels_texture;
      pixels_texture.create(width, height);
      sf::Sprite pixels_sprite(pixels_texture);
      sf::Clock clock;
      sf::VertexArray triangle(sf::Triangles, 3);
  
      triangle[0].position = sf::Vector2f(0, height);
      triangle[1].position = sf::Vector2f(width, height);
      triangle[2].position = sf::Vector2f(width/2, height-std::sqrt(std::pow(width,2)-std::pow(width/2,2)));
      triangle[0].color = sf::Color::Red;
      triangle[1].color = sf::Color::Blue;
      triangle[2].color = sf::Color::Green;
      
      
      while (window.isOpen()){
          sf::Event event;
          while (window.pollEvent(event)) {
              if (event.type == sf::Event::Closed) {
                  window.close();
              }
              if (event.type == sf::Event::KeyPressed &amp;&amp; event.key.code == sf::Keyboard::Escape) {
                  window.close();
              }
          }
  
          window.clear(sf::Color(255,255,255,255));
  
          // Pixel-by-pixel
          int us = duration_cast&lt; microseconds &gt;(system_clock::now().time_since_epoch()).count();
          for(int i=0;i!=width*height*4;++i){
              pixels[i]=255;
          }
          pixels_texture.update(pixels);
          window.draw(pixels_sprite);
          int duration=duration_cast&lt; microseconds &gt;(system_clock::now().time_since_epoch()).count()-us;
          std::cout&lt;&lt;&quot;Background: &quot;&lt;&lt;duration&lt;&lt;&quot; us\n&quot;;
  
          // Triangle
          us = duration_cast&lt; microseconds &gt;(system_clock::now().time_since_epoch()).count();
          window.draw(triangle);
           duration=duration_cast&lt; microseconds &gt;(system_clock::now().time_since_epoch()).count()-us;
          std::cout&lt;&lt;&quot;Triangle: &quot;&lt;&lt;duration&lt;&lt;&quot; us\n&quot;;
  
          window.display();
      }
      return EXIT_SUCCESS;
  }
  </code></pre>