title: Why is Ruby so much slower than Java?
body: <p>I was working on the Project Euler problems, when I stumbled on <a href="https://projecteuler.net/problem=14" rel="nofollow">this problem</a>. I wrote a program that solves it correctly in Ruby on Rails (new to the language):</p>
  
  <pre><code>class Collatz
   def search
      counter = 0
      storage = 0
      amount = 0
      (1..1000000).each do |i| i += 1
        temp = i
        while temp != 1
          temp = temp &amp; 1 == 1 ? 3 * temp + 1 : temp &gt;&gt; 1
          counter += 1
        end
        if counter &gt; amount
          amount = counter
          storage = i
        end
        counter = 0
      end
      puts storage
    end
  end
  
  start = Time.now
  collatz = Collatz.new
  collatz.search
  ending = Time.now
  puts "Time: #{ending - start}"
  </code></pre>
  
  <p>I realized that it took a very long time, 15.185317 seconds to be exact. 
  When I try Java, however, the time is much shorter:</p>
  
  <pre><code>public class Euler{
  
      private static void problem() {
          long a;
          int j;
          int max = 0;
          int maxnr = 0;
          for(int i = 1; i &lt; 1000000; i++) {
              a = i;
              j = 1;
              while( a != 1 ) {
                  a = ((a &amp; 1) == 1) ? (3 * a + 1) : (a &gt;&gt; 1);
                  j++;
              }
              if(j &gt; max) {
                  max = j;
                  maxnr = i;
              }
          }
          System.out.println(maxnr);
      }
  
      public static void main(String[] args) {
          long ct = System.currentTimeMillis();
          problem();
          long nt = System.currentTimeMillis() - ct;
          System.out.println(nt);
  }
  </code></pre>
  
  <p>In the end this program took 769 milliseconds. I am using RubyMine for Ruby and Intellij IDEA for Java. I can't seem to figure out why it takes so long, and I'm not sure if it is normal or not. The task doesn't seem too difficult, since I'm just looping. </p>