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
|
# File 'lib/asymptotic/graph.rb', line 10
def plot
Gnuplot.open do |gnuplot|
Gnuplot::Plot.new(gnuplot) do |plot|
plot.title "Average Runtime Analysis of #{@problem} (#{`ruby -v`.split(' (').first})"
plot.xlabel "Input size"
plot.ylabel "Average time taken in seconds (ran #{@attempts} times)"
@algorithm_hash.each do |name, function_hash|
seeds = function_hash[:input_seeds]
input_generation_function = function_hash[:input_function]
function = function_hash[:function]
sizes = []
puts "\nRunning benchmarks on: #{name}".green
runtimes = seeds.map do |seed|
size = 0
times_taken = ([0] * @attempts).map do
input = input_generation_function.(seed)
GC.disable
time_taken = Benchmark.realtime { function[input] }
GC.enable
size = input.size
time_taken
end
sizes << size
print '.'.yellow
average_time_taken = times_taken.inject(:+) / @attempts.to_f
end
points = [sizes, runtimes]
plot.data << Gnuplot::DataSet.new(points) do |set|
set.with = "linespoints"
set.title = name
end
end
end
end
end
|