Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active December 12, 2015 05:49
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/4724778 to your computer and use it in GitHub Desktop.
Save havenwood/4724778 to your computer and use it in GitHub Desktop.
Entirely Unscientific Benchmark of Primes in Various Ruby Implementations

Unscientific Benchmark

The Benchmark

def is_prime? n
  (2...n).all? { |i| n % i != 0 }
end

def sexy_primes n
  (9..n).map do |i|
    [i - 6, i]
  end.select do |i|
    i.all? { |i| is_prime? i }
  end
end

a = Time.now
sexy_primes 100_000
b = Time.now

puts b - a

The Results

Topaz-dev

7.78 sec

Ruby-1.9.3-p392

45.38 sec

Ruby-2.0.0-p0

44.82 sec

JRuby-1.7.3

41.97 sec

Rbx-2.0.0-rc1

85.51 sec

Maglev-1.1.0-dev

23.97 sec

MacRuby-0.1.3

35.37 sec

MRuby-dev

262.25 sec (not compiled or performance optimized, just here for fun)

IronRuby-1.1.4 on Mono-2.10.10

429.36 sec

Copyright Shannon Skipper MIT or Ruby License

@ko1
Copy link

ko1 commented Feb 8, 2013

Can I add this script to MRI benchmark directory? (license?)

@asterite
Copy link

asterite commented Feb 8, 2013

@havenwood
Copy link
Author

@ko1 Sorry for such a late reply, I just now saw your comment! Yes, please use the script for anything you'd like with any license you'd prefer!

@havenwood
Copy link
Author

@asterite Wow, cool! I need to take a closer look at crystal for sure.

@asterite
Copy link

@havenwood Thanks :-)

Just a small warning: Crystal is not a Ruby compiler. It's a different language (that happens to have almost the same syntax). And it's more limited than Ruby. And it's still in it's early development stage. And we don't know if it will work.

Topaz looks really good too. It's amazing that it's so fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment