Created
December 25, 2011 23:15
-
-
Save m0wfo/1519961 to your computer and use it in GitHub Desktop.
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 'java' | |
java_import java.util.concurrent.Executors | |
java_import java.util.concurrent.Callable | |
java_import java.lang.Runtime | |
java_import java.lang.System | |
$cores = Runtime.getRuntime.availableProcessors | |
queue = Executors.newFixedThreadPool($cores) | |
def benchmark(&work) | |
before = System.nanoTime | |
3.times { work.call } | |
after = System.nanoTime | |
p "That took #{(after - before)/1E9}s" | |
end | |
class Task | |
include Callable | |
def initialize(&block) | |
@work = block | |
end | |
def call | |
@work.call | |
end | |
end | |
class Array | |
def pmap(executor, &block) | |
# Parcel out the work into chunks to be executed sequentially | |
tasks = self.each_slice(self.size / $cores).map do |slice| | |
Task.new { slice.map {|i| block.call(i) } } | |
end | |
# Execute them all, block until they're done | |
results = executor.invokeAll(tasks) | |
# Dereference and merge all the FutureTasks | |
results.reduce([]) { |memo,obj| memo + obj.get } | |
end | |
end | |
a = (0..1E6).to_a | |
p 'Splendid new Array#pmap' | |
benchmark { a.pmap(queue) { |i| i += 1 } } | |
p 'Plain old sequential Array#map' | |
benchmark { a.map { |i| i += 1 } } | |
queue.shutdown |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment