Skip to content

Commit 5ae4fe7

Browse files
committed
Use Threads
FINISHED in 6.112150547s. RESULT = 0bbe9ecf251ef4131dd43e1600742cfb
1 parent be41568 commit 5ae4fe7

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

client.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,73 @@
1515
# - одновременно можно запускать не более одного
1616
#
1717
def a(value)
18-
puts "https://localhost:9292/a?value=#{value}"
19-
Faraday.get("https://localhost:9292/a?value=#{value}").body
18+
Thread.new do
19+
puts "https://localhost:9292/a?value=#{value}"
20+
Faraday.get("https://localhost:9292/a?value=#{value}").body
21+
end
2022
end
2123

2224
def b(value)
23-
puts "https://localhost:9292/b?value=#{value}"
24-
Faraday.get("https://localhost:9292/b?value=#{value}").body
25+
Thread.new do
26+
puts "https://localhost:9292/b?value=#{value}"
27+
Faraday.get("https://localhost:9292/b?value=#{value}").body
28+
end
2529
end
2630

2731
def c(value)
28-
puts "https://localhost:9292/c?value=#{value}"
29-
Faraday.get("https://localhost:9292/c?value=#{value}").body
32+
Thread.new do
33+
puts "https://localhost:9292/c?value=#{value}"
34+
Faraday.get("https://localhost:9292/c?value=#{value}").body
35+
end
3036
end
3137

3238
# Референсное решение, приведённое ниже работает правильно, занимает ~19.5 секунд
3339
# Надо сделать в пределах 7 секунд
3440

3541
def collect_sorted(arr)
36-
arr.sort.join('-')
42+
arr.map(&:value).sort.join('-')
3743
end
3844

3945
start = Time.now
4046

47+
### 1 queue
4148
a11 = a(11)
4249
a12 = a(12)
4350
a13 = a(13)
4451
b1 = b(1)
52+
b2 = b(2)
4553

46-
ab1 = "#{collect_sorted([a11, a12, a13])}-#{b1}"
54+
ab1 = "#{collect_sorted([a11, a12, a13])}-#{b1.value}"
4755
puts "AB1 = #{ab1}"
4856

4957
c1 = c(ab1)
5058
puts "C1 = #{c1}"
5159

60+
### 2 queue
5261
a21 = a(21)
5362
a22 = a(22)
5463
a23 = a(23)
55-
b2 = b(2)
64+
b3 = b(3)
5665

57-
ab2 = "#{collect_sorted([a21, a22, a23])}-#{b2}"
66+
ab2 = "#{collect_sorted([a21, a22, a23])}-#{b2.value}"
5867
puts "AB2 = #{ab2}"
5968

6069
c2 = c(ab2)
6170
puts "C2 = #{c2}"
6271

72+
### 3 queue
6373
a31 = a(31)
6474
a32 = a(32)
6575
a33 = a(33)
66-
b3 = b(3)
6776

68-
ab3 = "#{collect_sorted([a31, a32, a33])}-#{b3}"
77+
ab3 = "#{collect_sorted([a31, a32, a33])}-#{b3.value}"
6978
puts "AB3 = #{ab3}"
7079

7180
c3 = c(ab3)
7281
puts "C3 = #{c3}"
7382

7483
c123 = collect_sorted([c1, c2, c3])
75-
result = a(c123)
84+
result = a(c123).value
7685

7786
puts "FINISHED in #{Time.now - start}s."
7887
puts "RESULT = #{result}" # 0bbe9ecf251ef4131dd43e1600742cfb

rails-optimization-task5.png

63.9 KB
Loading

0 commit comments

Comments
 (0)