title: Slow array access in ruby?
body: <p>I would like to present you this small (in this reduced form senseless) snippet of ruby code, which runs awfully slow:</p>
  
  <pre><code>MAX = 28123
  M = 7000
  
  abundant = Array.new(M) {|k| k}
  checked = Array.new(MAX) {|k| false}
  
  for a in 0..M-1 do
      for b in a..M-1 do
          checked[abundant[a] + abundant[b]] = true if abundant[a] + abundant[b] &lt; MAX
      end
  end
  </code></pre>
  
  <p>It takes about 10 seconds to execute, whereas the equivalent C++ code runs in about 0.2 secs:</p>
  
  <pre><code>int main()
  {
      int abundant[M];
      bool checked[MAX];               
  
      for (int n = 0; n &lt; M; n++)
          abundant[n] = n;
      for (int n = 0; n &lt; MAX; n++)
          checked[n] = false;
  
      for (int a = 0; a &lt; M; a++)
          for (int b = a; b &lt; M; b++)
              if (abundant[a] + abundant[b] &lt; MAX)
                  checked[abundant[a] + abundant[b]] = true;
  }
  </code></pre>
  
  <p>What am I doing wrong in the ruby implementation? I'm new to ruby - do I use any statement which is known to be thaaat slow?</p>