Skip to content

Instantly share code, notes, and snippets.

Last active December 12, 2015 05:49
Show Gist options
  • 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 }

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

a =
sexy_primes 100_000
b =

puts b - a

The Results


7.78 sec


45.38 sec


44.82 sec


41.97 sec


85.51 sec


23.97 sec


35.37 sec


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

Copy link

ko1 commented Feb 8, 2013

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

Copy link

asterite commented Feb 8, 2013

Copy link

@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!

Copy link

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

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