Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Created April 25, 2015 18:40
Show Gist options
  • Save tiagopog/b075dc39f5f6629d9f72 to your computer and use it in GitHub Desktop.
Save tiagopog/b075dc39f5f6629d9f72 to your computer and use it in GitHub Desktop.
Parallelism with Ruby
require 'benchmark'
require 'parallel'
def expensive_calculation(letter)
letter *= 1_000_000_000
puts letter[rand(0..999_999_999)]
end
Benchmark.bm do |x|
x.report("With Parallel: \n", in_thread: 3) do
Parallel.map(%w(a b c)) do |letter|
expensive_calculation(letter)
end
end
x.report("Without Parallel: \n") do
%w(a b c).map do |letter|
expensive_calculation(letter)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment