Skip to content

Instantly share code, notes, and snippets.

@bibendi
Created July 8, 2018 16:34
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 bibendi/79c09b093cc07644aea2ccea2f7b0bb0 to your computer and use it in GitHub Desktop.
Save bibendi/79c09b093cc07644aea2ccea2f7b0bb0 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'open-uri'
require 'bigdecimal'
require 'bigdecimal/math'
class Contract
DIGITS = 10_000
URL = 'http://www.nooooooooooooooo.com/'.freeze
def call(parallel_io_only:, cpu_count:, io_count:)
threads = []
io_count.times { threads << Thread.new { io_meth } }
if parallel_io_only
cpu_count.times { cpu_meth }
else
cpu_count.times { threads << Thread.new { cpu_meth } }
end
threads.each(&:join)
end
private
def cpu_meth
BigMath.PI(DIGITS)
end
def io_meth
open(URL)
end
end
[
{cpu_count: 2, io_count: 6},
{cpu_count: 3, io_count: 3},
{cpu_count: 6, io_count: 2}
].each do |options|
Benchmark.ips do |x|
x.report("parallel_io_only: true, options: #{options}") do
::Contract.new.call(parallel_io_only: true, **options)
end
x.report("parallel_io_only: false, options: #{options}") do
::Contract.new.call(parallel_io_only: false, **options)
end
x.compare!
end
end
@bibendi
Copy link
Author

bibendi commented Jul 8, 2018

Warming up --------------------------------------
parallel_io_only: true, options: {:cpu_count=>2, :io_count=>6}
                         1.000  i/100ms
parallel_io_only: false, options: {:cpu_count=>2, :io_count=>6}
                         1.000  i/100ms
Calculating -------------------------------------
parallel_io_only: true, options: {:cpu_count=>2, :io_count=>6}
                          0.996  (± 0.0%) i/s -      5.000  in   5.021074s
parallel_io_only: false, options: {:cpu_count=>2, :io_count=>6}
                          0.886  (± 0.0%) i/s -      5.000  in   5.668470s

Comparison:
parallel_io_only: true, options: {:cpu_count=>2, :io_count=>6}:        1.0 i/s
parallel_io_only: false, options: {:cpu_count=>2, :io_count=>6}:        0.9 i/s - 1.12x  slower

Warming up --------------------------------------
parallel_io_only: true, options: {:cpu_count=>3, :io_count=>3}
                         1.000  i/100ms
parallel_io_only: false, options: {:cpu_count=>3, :io_count=>3}
                         1.000  i/100ms
Calculating -------------------------------------
parallel_io_only: true, options: {:cpu_count=>3, :io_count=>3}
                          0.668  (± 0.0%) i/s -      4.000  in   5.983861s
parallel_io_only: false, options: {:cpu_count=>3, :io_count=>3}
                          0.629  (± 0.0%) i/s -      4.000  in   6.371746s

Comparison:
parallel_io_only: true, options: {:cpu_count=>3, :io_count=>3}:        0.7 i/s
parallel_io_only: false, options: {:cpu_count=>3, :io_count=>3}:        0.6 i/s - 1.06x  slower

Warming up --------------------------------------
parallel_io_only: true, options: {:cpu_count=>6, :io_count=>2}
                         1.000  i/100ms
parallel_io_only: false, options: {:cpu_count=>6, :io_count=>2}
                         1.000  i/100ms
Calculating -------------------------------------
parallel_io_only: true, options: {:cpu_count=>6, :io_count=>2}
                          0.336  (± 0.0%) i/s -      2.000  in   5.959183s
parallel_io_only: false, options: {:cpu_count=>6, :io_count=>2}
                          0.332  (± 0.0%) i/s -      2.000  in   6.025768s

Comparison:
parallel_io_only: true, options: {:cpu_count=>6, :io_count=>2}:        0.3 i/s
parallel_io_only: false, options: {:cpu_count=>6, :io_count=>2}:        0.3 i/s - 1.01x  slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment