-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGIL_in_ruby.rb
53 lines (46 loc) · 1.03 KB
/
GIL_in_ruby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'benchmark'
@threads = []
Benchmark.bm(14) do |x|
x.report('single-thread') do
8.times do
tmp_array = []
10_000_000.times { |n| tmp_array << n }
end
end
x.report('multi-thread') do
8.times do
@threads << Thread.new do
tmp_array = []
10_000_000.times { |n| tmp_array << n }
end
end
@threads.each(&:join)
end
end
#
# ```
# user system total real
# single-thread
# 5.240000 0.580000 5.820000 ( 5.880398)
# multi-thread
# 6.450000 0.710000 7.160000 ( 7.234476)
#
# ```
require 'faraday'
@conn = Faraday.new(url: 'https://ruby-china.org')
@threads = []
Benchmark.bm(14) do |x|
x.report('single-thread') do
20.times { @conn.get }
end
x.report('multi-thread') do
20.times do
@threads << Thread.new { @conn.get }
end
@threads.each(&:join)
end
end
# user system total real
# single-thread
# 0.190000 0.020000 0.210000 ( 4.345562)
# multi-thread 0.090000 0.020000 0.110000 ( 0.269009)