Class: Asymptotic::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/asymptotic/graph.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attempts = 5, problem, algorithm_hash) ⇒ Graph



4
5
6
7
8
# File 'lib/asymptotic/graph.rb', line 4

def initialize(attempts = 5, problem, algorithm_hash)
  @attempts = attempts
  @problem = problem
  @algorithm_hash = algorithm_hash
end

Class Method Details

.plot(*args) ⇒ Object



51
52
53
# File 'lib/asymptotic/graph.rb', line 51

def self.plot(*args)
  self.new(*args).plot
end

Instance Method Details

#plotObject



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
# File 'lib/asymptotic/graph.rb', line 10

def plot
  Gnuplot.open do |gnuplot|
    Gnuplot::Plot.new(gnuplot) do |plot|
      plot.title "Asymptotic Analysis of #{@problem} (#{`ruby -v`.split(' (').first})"
      plot.xlabel "Input size"
      plot.ylabel "Time taken in seconds"


      @algorithm_hash.each do |name, function_hash|
        puts "\nRunning benchmarks on: #{name}".green
        seeds = function_hash[:input_seeds]
        input_generation_function = function_hash[:input_function]
        function = function_hash[:function]
        sizes = []
        runtimes = seeds.map do |seed|
          size = 0
          times_taken = ([0] * @attempts).map do
            input = input_generation_function.(seed)
            size = input.size
            GC.disable
            time_taken = Benchmark.realtime { function[input] }
            GC.enable
            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