-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.rb
60 lines (54 loc) · 1.47 KB
/
benchmark.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
54
55
56
57
58
59
60
require 'benchmark'
require_relative './chart.rb'
module HackerRank
module Benchmarking
def self.write_benchmark_results_to_csv(filename:, x_label:, y_label:, data:)
File.open(filename, 'w+') do |f|
f.puts("#{x_label},#{y_label}")
data.each do |x, y|
f.puts("#{x},#{y}")
end
end
end
def self.benchmarking_n_times(fn = HackerRank::Solutions.method(:method_name), n0: 0, n_max: 5, step: 1, &block)
n_axis = []
time_axis = []
Benchmark.benchmark do |x|
n0.step(n_max, step) do |n|
time = x.report("n = #{n}") { fn.call(n) }
n_axis << n
time_axis << time.real
end
end
yield [n_axis, time_axis]
end
def self.create_time_complexity_analysis(
method_name:,
file_name:,
n_max:,
n0: 0,
n_incrementation_step: 1,
space_between_chart_items: 1
)
Benchmarking.benchmarking_n_times(
method(method_name),
n0: n0,
n_max: n_max,
step: n_incrementation_step
) do |n_values, time_results|
results = ChartUtils.prepare_chart_data_with(
x_axis: n_values,
y_axis: time_results,
max_x_size: time_results.size,
step: space_between_chart_items
)
Benchmarking.write_benchmark_results_to_csv(
filename: file_name,
x_label: 'n',
y_label: 'T(n)',
data: results
)
end
end
end
end