Class: Graph::Function::Comparison
- Inherits:
-
Object
- Object
- Graph::Function::Comparison
- Includes:
- PlotConfig, ReformatString
- Defined in:
- lib/graph/function/comparison.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data_generator ⇒ Object
Returns the value of attribute data_generator.
Instance Method Summary collapse
-
#initialize(generator) ⇒ Comparison
constructor
A new instance of Comparison.
- #of(*functions) ⇒ Object
Methods included from PlotConfig
Methods included from ReformatString
#camel_title, #escape_underscores, #extract_filename
Constructor Details
#initialize(generator) ⇒ Comparison
Returns a new instance of Comparison.
8 9 10 |
# File 'lib/graph/function/comparison.rb', line 8 def initialize(generator) @data_generator = generator end |
Instance Attribute Details
#data_generator ⇒ Object
Returns the value of attribute data_generator.
6 7 8 |
# File 'lib/graph/function/comparison.rb', line 6 def data_generator @data_generator end |
Instance Method Details
#of(*functions) ⇒ Object
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 |
# File 'lib/graph/function/comparison.rb', line 12 def of(*functions) fail unless functions.all? {|f| f.respond_to?(:call) } results = {} Gnuplot.open do |gp| Gnuplot::Plot.new(gp) do |plot| title = functions_to_title(functions) plot.title title set_up(plot) x = Graph::Function.configuration.step trials = Graph::Function.configuration.trials pb = ProgressBar.create(title: title, total: x.size) functions.each do |f| pb.reset name = fname(f) results[name] = {} y = x.collect do |v| pb.increment data = data_generator.call(v) current_trials = (1..trials).collect do |_| Benchmark.measure { f.call(data) }.real end results[name][v] = current_trials current_trials.reduce(0.0, :+) / trials end plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "linespoints" ds.title = "#{escape_underscores(name)}" end end end end results end |