Skip to content

Commit 9282214

Browse files
committed
add simple benchmark
1 parent aded6dd commit 9282214

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

week3/quicksort/interview-questions/task1/solution.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,19 @@ def sort(array1, array2, start: nil, finish: nil)
129129

130130
n = 10
131131

132-
bolt_pile = (0...n).map { |l| Bolt.new(l) }.shuffle
133-
nut_pile = (0...n).map { |l| Nut.new(l) }.shuffle
132+
def test(n)
133+
bolt_pile = (0...n).map { |l| Bolt.new(l) }.shuffle
134+
nut_pile = (0...n).map { |l| Nut.new(l) }.shuffle
135+
sort(bolt_pile, nut_pile)
136+
bolt_pile.zip(nut_pile).all? { |bolt, nut| bolt == nut }
137+
end
138+
139+
require "benchmark"
140+
puts Benchmark.measure { "a"*1_000_000_000 }
134141

135-
p [bolt_pile, nut_pile]
136-
sort(bolt_pile, nut_pile)
137-
p [bolt_pile, nut_pile]
138-
p bolt_pile.zip(nut_pile).map { |bolt, nut| bolt == nut }.uniq
142+
h = {}
143+
[100, 1000, 10000, 1000000].each do |n|
144+
b = Benchmark.measure { raise unless test(n) }
145+
h[n] = b.real
146+
end
147+
p h

0 commit comments

Comments
 (0)