Skip to content

Instantly share code, notes, and snippets.

@kshahkshah
Last active October 5, 2016 20:37
Show Gist options
  • Save kshahkshah/2f97a99c7b7f02970a1a87f4770353ae to your computer and use it in GitHub Desktop.
Save kshahkshah/2f97a99c7b7f02970a1a87f4770353ae to your computer and use it in GitHub Desktop.
require 'concurrent'
require 'benchmark'
def process_sheets(sheets)
all_dependencies = Concurrent::Array.new
sheets.each_pair do |name, rows|
rows.each do |val|
all_dependencies.push(Concurrent::Future.execute {val*2})
end
end
ftr = Concurrent::dataflow(*all_dependencies) {|*vals| vals.reduce(:+) }
ftr.value
# puts ftr.value
# puts ftr.rejected?
# puts ftr.reason
end
spread_1 = {
"A" => Concurrent::Array.new(10){|i|i},
"B" => Concurrent::Array.new(10){|i|i},
"C" => Concurrent::Array.new(10){|i|i}
}
spread_2 = {
"A" => Concurrent::Array.new(100){|i|i},
"B" => Concurrent::Array.new(100){|i|i},
"C" => Concurrent::Array.new(100){|i|i}
}
spread_3 = {
"A" => Concurrent::Array.new(1000){|i|i},
"B" => Concurrent::Array.new(1000){|i|i},
"C" => Concurrent::Array.new(1000){|i|i}
}
spread_4 = {
"A" => Concurrent::Array.new(10000){|i|i},
"B" => Concurrent::Array.new(10000){|i|i},
"C" => Concurrent::Array.new(10000){|i|i}
}
Benchmark.bm do |x|
x.report { process_sheets(spread_1) }
x.report { process_sheets(spread_2) }
x.report { process_sheets(spread_3) }
x.report { process_sheets(spread_4) }
end
# ruby 2.3
# user system total real
# 0.010000 0.010000 0.020000 ( 0.023191)
# 0.080000 0.020000 0.100000 ( 0.103426)
# 0.630000 0.210000 0.840000 ( 0.791183)
# 5.280000 2.170000 7.450000 ( 6.713608)
# jruby 9.1.5.0 (2.3.1) 2016-09-07 036ce39 Java HotSpot(TM) 64-Bit Server VM 25.71-b15 on 1.8.0_71-b15 +jit [darwin-x86_64]
# user system total real
# 0.360000 0.010000 0.370000 ( 0.425416)
# 0.790000 0.030000 0.820000 ( 0.617765)
# 2.560000 0.050000 2.610000 ( 1.574381)
# 9.540000 0.240000 9.780000 ( 5.557661)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment