Skip to content

Instantly share code, notes, and snippets.

@hoguej
Created June 6, 2013 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoguej/5719075 to your computer and use it in GitHub Desktop.
Save hoguej/5719075 to your computer and use it in GitHub Desktop.
require 'parallel'
require 'date'
require './pieces' # adds pieces function to Array objects
@things = []
100_000.times do |i|
@things << { name: i.to_s, value: i }
end
# run 1
@sum = 0
start = Time.now
@sum = @things.map{ |t| t[:value] }.reduce(:+)
duration = Time.now - start
puts @sum
puts duration
# run 16 times
(1..50).each do |procs|
(1..50).each do |pieces|
start = Time.now
@sum = Parallel.map( @things.pieces(pieces), :in_processes => procs ) { |subarray|
subarray.map{ |thing| thing[:value] }.reduce(:+)
}.reduce(:+)
duration = Time.now - start
puts "duration:#{duration} procs:#{procs} pieces:#{pieces}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment