Created
April 25, 2015 18:40
-
-
Save tiagopog/b075dc39f5f6629d9f72 to your computer and use it in GitHub Desktop.
Parallelism with Ruby
This file contains 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
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