title: Optimizing Code - CPU vs Memory
body: <p>I'm trying to optimize some algorithms in the core of my program and I'm wondering what part of the hardware really "slows down" everything.</p>
  
  <p>The probably two most important parts of the hardware when it comes to programming are the CPU and the RAM (and the Cache memory of the CPU???). But how do they play together when it comes to performance?</p>
  
  <p>Consider this block of code:</p>
  
  <pre><code>std::vector&lt;int&gt; vec;
  for(int count = 0; count &lt; 100000; count++)
  {
      vec.push_back(count);
  }
  </code></pre>
  
  <p>In this case, the part which really slows down the algorithm is the speed at which memory is  written to the RAM(?). The speed of the CPU is hardly going to take any impact on the execution time of this code, since there's not much to calculate, right?</p>
  
  <p>But now, consider this block of code:</p>
  
  <pre><code>std::vector&lt;int&gt; vec;
  for(int count = 0; count &lt; 100000; count++)
  {
      vec.push_back(count/10/10/10/10);
  }
  </code></pre>
  
  <p>In this case, several arithmetic operations are executed (on the CPU) before inserted into the list (on the RAM). Is there going to be a big performance impact in this case? The CPU has to first calculate the result and THEN the result can be written to the RAM, so it has to be definitely slower, right?</p>
  
  <p>If this is correct, then (as far as I see it) the part which REALLY slows down programs is  the SPEED at which can be written to the RAM and not the speed of the CPU, right? Because even if have a more complex block of code, it will always be in the form:</p>
  
  <pre><code>//calculate something and write it to the RAM
  int something = calculateSomething();
  </code></pre>
  
  <p>There will always be arithmetic operations, as well as memory allocations. But I rarely see the CPU being working on 100 % when looking at the Task Manager - which concludes that there has to be another part which slows down the whole thing.</p>
  
  <p>To bring it to the point:
  What is the most important thing I have to consider when optimizing an algorithm? Allocating less memory and recalculating values more often which increases CPU work and decreases memory allocation - or calculation values only once and writing them to the memory? What is going to bring me more performance out of my algorithm?
  And what are the parts in modern hardware today that REALLY slow down code to be as fast as it is?</p>
  
  <p>Thanks already in advance.</p>