Created
April 16, 2013 05:22
-
-
Save artofhuman/5393566 to your computer and use it in GitHub Desktop.
pmap
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
# coding utf-8 | |
class Array | |
def pmap | |
pipes, out = [], [] | |
each do |e| | |
r, w = IO.pipe | |
Process.fork do | |
r.close | |
res = yield(e) | |
w.write(Marshal.dump(res)) | |
end | |
w.close | |
pipes << r | |
end | |
Process.waitall | |
pipes.each do |p| | |
out << Marshal.load(p.read) | |
p.close | |
end | |
out | |
end # end pmap | |
end # end Array | |
require 'benchmark' | |
seconds = Benchmark.realtime do | |
test = [1, 2, 3].pmap do |x| | |
sleep x | |
x**x | |
end | |
p test | |
end | |
puts "work #{seconds} seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment