Skip to content

Instantly share code, notes, and snippets.

@jkutner
Created March 16, 2012 00:31
Show Gist options
  • Save jkutner/2047877 to your computer and use it in GitHub Desktop.
Save jkutner/2047877 to your computer and use it in GitHub Desktop.
Trying running these programs on MRI and JRuby!
require 'benchmark'
def factorial_repeat(k, x)
k.times { x.downto(1).inject(:*) }
end
def run(i, k, x)
i.downto(1).map do
Thread.new { factorial_repeat(k, x) }
end.each { |t| t.join }
end
puts Benchmark.measure { run(1, 400_000, 99) }
puts Benchmark.measure { run(4, 100_000, 99) }
require 'benchmark'
def run(x)
data = []
(1..x).map do |m|
Thread.new do
x.times do |n|
data << m * n
end
end
end.each {|t| t.join}
puts "size=#{data.size}"
puts "nils=#{data.size-data.compact.size}"
end
puts Benchmark.measure { run(32) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment