title: Performance in different variable scopes?
body: <p>I wrote a simple test case:</p>
  
  <pre><code>$g = 10
  def fun_g a
    a += $g
  end
  
  def fun_l a
    l = 10
    a += l
  end
  
  def fun_gl a
    $gl = 10
    a += $gl
  end
  
  def test fun
    t = Time.now
  
    (10 ** 6).times { |n|
      method(fun).call(n)
    }
  
    puts(Time.now - t)
  end
  
  test :fun_g
  test :fun_l
  test :fun_gl
  </code></pre>
  
  <p>The result is something like below. It seems that the first function <code>fun_g</code> is the fastest, and the <code>fun_gl</code> is the slowest.</p>
  
  <pre><code>1.249626
  1.268355
  1.30267
  </code></pre>
  
  <p>But I also get result that indicates the <code>fun_l</code> is the fastest.</p>
  
  <pre><code>1.27436
  1.25794
  1.303973
  </code></pre>
  
  <p>Theoretically, which one should be the fastest?
  What if I change the example to a compiled language, will the result still be the same? Will the compiler optimize the local variables to global ones?</p>