Skip to content

Instantly share code, notes, and snippets.

@m0wfo m0wfo/gist:1519961
Created Dec 25, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.