Skip to content

Instantly share code, notes, and snippets.

@utkarshrai003
Created May 1, 2018 05:17
Show Gist options
  • Save utkarshrai003/e42cf8afe0d7c1fee14aa87c00f1c315 to your computer and use it in GitHub Desktop.
Save utkarshrai003/e42cf8afe0d7c1fee14aa87c00f1c315 to your computer and use it in GitHub Desktop.
ruby version -> ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin17]
# Sequential processing
require 'benchmark'
require 'open-uri'
require 'json'
Benchmark.bm do |x|
x.report('without parallel processing - ') do
100.times do |i|
response = open('http://ip.jsontest.com/').read
data = JSON.parse(response)['ip']
puts data
end
end
end
Time noted -> 96 sec
# With forking (multi processing)
require 'benchmark'
require 'open-uri'
require 'json'
Benchmark.bm do |x|
x.report('with parallel processing - ') do
100.times do |i|
fork do
response = open('http://ip.jsontest.com/').read
data = JSON.parse(response)['ip']
puts data
end
end
end
end
Time noted -> 1.06 sec
# With Multi threading
require 'benchmark'
require 'open-uri'
require 'json'
threads = []
Benchmark.bm do |x|
x.report('with multiple threading - ') do
100.times do |i|
threads << Thread.new do
response = open('http://ip.jsontest.com/').read
data = JSON.parse(response)['ip']
puts data
end
end
threads.map(&:join)
end
end
Time Noted -> 6.39 sec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment