Created
March 28, 2012 22:34
-
-
Save chrisrobinson/2231141 to your computer and use it in GitHub Desktop.
Royal Flushes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
royal_flushes = [ | |
[1,10,11,12,13], | |
[14,23,24,25,26], | |
[27,36,37,38,39], | |
[40,49,50,51,52]] | |
iterations = 50_000_000 | |
matches = 0 | |
deck = (1..52).to_a | |
iterations.times do | |
matches += 1 if royal_flushes.include?(deck.sample(5).sort) | |
end | |
puts "#{matches}/#{iterations}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'thread' | |
royal_flushes = [ | |
[1,10,11,12,13], | |
[14,23,24,25,26], | |
[27,36,37,38,39], | |
[40,49,50,51,52]] | |
iterations = 50_000_000 | |
matches = 0 | |
thread_count = 3 | |
iterations_per_thread = iterations/thread_count | |
semaphore = Mutex.new | |
threads = ThreadGroup.new | |
1.upto(thread_count) do | |
thread = Thread.new do | |
deck = (1..52).to_a | |
iterations_per_thread.times do | |
semaphore.synchronize { matches += 1 } if royal_flushes.include?(deck.sample(5).sort) | |
end | |
end | |
threads.add thread | |
end | |
until threads.list.empty? do | |
sleep 0.1 | |
end | |
puts "#{matches}/#{iterations}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chris@ubuntu64:~/2231141$ ruby --1.9 -v | |
jruby 1.6.7 (ruby-1.9.2-p312) (2012-02-22 3e82bc8) (OpenJDK 64-Bit Server VM 1.7.0_147-icedtea) [linux-amd64-java] | |
chris@ubuntu64:~/2231141$ time ruby --1.9 royalflush.rb ; time ruby --1.9 royalflush2.rb | |
37/30000000 | |
real 0m42.600s | |
user 0m43.543s | |
sys 0m1.928s | |
41/30000000 | |
real 0m29.667s | |
user 0m57.580s | |
sys 0m1.620s | |
chris@ubuntu64:~/2231141$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment