|
6 | 6 | require 'benchmark/plot/version' |
7 | 7 |
|
8 | 8 | module Benchmark |
| 9 | + # Create a plot of the benchmarked code over a varied number of inputs. |
| 10 | + # |
| 11 | + # @param test_data [Enumerable] An object containing the data that is to be |
| 12 | + # used for the benchmarking. This object must be Enumerable, i.e. it must |
| 13 | + # have the `#each` method defined. Each of the constituent objects of |
| 14 | + # `test_data` should have a `#to_s` method defined, which will be used for |
| 15 | + # populating the X axis labels. |
| 16 | + # @param [Hash] opts The options for configuring the graph. |
| 17 | + # @option opts [Symbol, String] :title (:Benchmarks) Title of the graph. |
| 18 | + # @option opts [Symbol, String] :file_name (:benchmark_plot_graph) Name of |
| 19 | + # file to which plot is written. |
| 20 | + # @option opts [Symbol] :time (:real) The kind of time that should be displayed |
| 21 | + # on the graph. Can be `:real` for elapsed real time, `:stime` for System |
| 22 | + # CPU time, `:utime` for User CPU time, `:cstime` for System CPU time of children, |
| 23 | + # `:cutime` for User CPU time of children, and `:total` for the total time |
| 24 | + # that is `:utime + :stime + :cutime + :cstime`. |
| 25 | + # @option opts [TrueClass, FalseClass] :x_labels (true) Whether you want X-axis |
| 26 | + # labels or not. |
| 27 | + # @option opts [String, Symbol] :x_axis_label (nil) X-axis label string. |
| 28 | + # @example Benchmark map{}.flatten vs. flat_map |
| 29 | + # require 'benchmark/plot' |
| 30 | + # class TestArray |
| 31 | + # attr_reader :arr |
| 32 | + # |
| 33 | + # def initialize arr |
| 34 | + # @arr = arr |
| 35 | + # end |
| 36 | + # |
| 37 | + # # The to_s method is called for populating the X axis labels. |
| 38 | + # def to_s |
| 39 | + # @arr.size.to_s |
| 40 | + # end |
| 41 | + # end |
| 42 | + # |
| 43 | + # test_data = [5, 25, 50, 75, 100, 125, 150, 175, 200,250,300] |
| 44 | + # test_data.map! {|e| TestArray.new(Array.new(e) {|i| i}) } |
| 45 | + # |
| 46 | + # Benchmark.plot(test_data) do |x| |
| 47 | + # x.report("map.flatten") do |data| |
| 48 | + # # Here `data` will be of an object of type `TestArray`. |
| 49 | + # data.arr.map { [nil] }.flatten |
| 50 | + # end |
| 51 | + # |
| 52 | + # x.report("flat_map") do |data| |
| 53 | + # data.arr.flat_map { [nil] } |
| 54 | + # end |
| 55 | + # end |
9 | 56 | def self.plot test_data, opts={}, &block |
10 | 57 | include Benchmark::Plot |
11 | 58 |
|
|
0 commit comments