Skip to content

Instantly share code, notes, and snippets.

@mlyubarskyy
Last active June 15, 2016 20:42
Show Gist options
  • Save mlyubarskyy/d389a92132549ff115c71bbb2e9976ad to your computer and use it in GitHub Desktop.
Save mlyubarskyy/d389a92132549ff115c71bbb2e9976ad to your computer and use it in GitHub Desktop.
class QuickieParallel
attr_accessor :results
def initialize(args)
@num_procs = args.fetch(:num_processes, 4)
@using_db = args.fetch(:using_db, false)
@debug = args.fetch(:debug, false)
@results = {}
end
def finish_callback(val, index, result)
@results[index] = result
end
def each(batch)
log_with_time("total time for batch", 0.0) do
Parallel.each_with_index(batch, in_processes: @num_procs, finish: method(:finish_callback)) do |value, index|
ActiveRecord::Base.connection.reconnect! if @num_procs > 1 && @using_db
log_with_time("yield ##{index}", 0.0) do
yield(value, index) if block_given?
end
end
end
end
private
def log_with_time(txt=nil, thld=100, &block)
start = Time.now
result = yield
taken = ((Time.now - start) * 1000).round(1)
dbg = ["===="]
dbg << txt.to_s.ljust(50) if txt
dbg << "(#{taken}ms)"
puts dbg.join(' ') if @debug && taken > thld
result
end
end
def fibonacci_r(a, b, n)
n == 0 ? a : fibonacci_r(b, a + b, n - 1)
end
def fibonacci(n)
fibonacci_r(0, 1, n)
end
# processes | time
# 0 | (7892.3ms)
# 2 | (6069.9ms)
# 4 | (4878.0ms)
processor = QuickieParallel.new(num_processes: 4, debug: true)
processor.each(5_000.times) do |val, index|
fibonacci(val)
end
processor.results.sort
# => [
# [0, 0],
# [1, 1],
# [2, 1],
# [3, 2],
# [4, 3],
# [5, 5],
# [6, 8],
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment