Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@m0wfo
Created December 25, 2011 23:15
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 m0wfo/1519961 to your computer and use it in GitHub Desktop.
Save m0wfo/1519961 to your computer and use it in GitHub Desktop.
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