public
Description: Prettier Benchmarking for Ruby
Homepage:
Clone URL: git://github.com/wycats/benchwarmer.git
benchwarmer / README
100644 118 lines (104 sloc) 4.087 kb
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# benchwarmer
# ===========
#
# A gem that provides a nice DSL for a common type of benchmark.
#
# Specifically:
 
  require "rubygems"
  require "benchwarmer"
  
  TIMES = 100_000
  
  Benchmark.warmer(TIMES) do # [1]
    columns :one, :two, :three # [2]
    titles :one => "Option 1", :three => "Option 3" # [3]
    
    group("Squeezing") do # [4]
      report "with #squeeze" do # [5]
        one { "abc//def//ghi//jkl".squeeze("/") } # [6]
        two { "abc///def///ghi///jkl".squeeze("/") }
        three { "abc////def////ghi////jkl".squeeze("/") }
      end
      report "with #gsub" do
        one { "abc//def//ghi//jkl".gsub(/\/+/, "/") }
        two { "abc///def///ghi///jkl".gsub(/\/+/, "/") }
        three { "abc////def////ghi////jkl".gsub(/\/+/, "/") }
      end
    end
    
    group("Spliting") do
      report "with #split" do
        one { "aaa/aaa/aaa.bbb.ccc.ddd".split(".") }
        two { "aaa//aaa//aaa.bbb.ccc.ddd.eee".split(".") }
        three { "aaa///aaa///aaa.bbb.ccc.ddd.eee.fff".split(".") }
      end
      report "with #match" do
        one { "aaa/aaa/aaa.bbb.ccc.ddd".match(/\.([^\.]*)$/) }
        two { "aaa//aaa//aaa.bbb.ccc.ddd.eee".match(/\.([^\.]*)$/) }
        three { "aaa///aaa///aaa.bbb.ccc.ddd.eee.fff".match(/\.([^\.]*)$/) }
      end
    end
  end
  
  # [1] Specify the number of times you want to run each benchmark
  # [2] Specify the column names. By default, they will be displayed as upcased
  # versions of themselves.
  # [3] Specify prettier titles via a Hash. You do *not* need to specify the
  # same column in both columns and titles.
  # [4] Specify a group of benchmarks you wish to run
  # [5] Specify the name of the report you wish to run
  # [6] Optionally, specify several different variants of the benchmark. This could
  # be useful if you're testing a new alorithm, and wish to compare a bunch of
  # benchmarks against each algorithms. In most cases, you will probably want to
  # use the variant below, which does not require columns.
  
  # Output:
  # Running the benchmarks 100000 times each...
  #
  # Option 1 | TWO | Option 3 |
  # -----------------------------------------------------
  # Squeezing with #squeeze 0.15 | 0.15 | 0.14 |
  # with #gsub 0.38 | 0.35 | 0.36 |
  # -----------------------------------------------------
  # Spliting with #split 0.43 | 0.51 | 0.61 |
  # with #match 0.29 | 0.35 | 0.38 |
  # -----------------------------------------------------
 
  Benchmark.warmer(TIMES) do
    group("Squeezing") do
      report "with #squeeze" do
        "abc//def//ghi//jkl".squeeze("/")
      end
      report "with #gsub" do
        "abc//def//ghi//jkl".gsub(/\/+/, "/")
      end
    end
    
    group("Spliting") do
      report "with #split" do
        "aaa/aaa/aaa.bbb.ccc.ddd".split(".")
      end
      report "with #match" do
        "aaa/aaa/aaa.bbb.ccc.ddd".match(/\.([^\.]*)$/)
      end
    end
  end
 
  # Output:
  # Running the benchmarks 100000 times each...
  #
  # Results |
  # ---------------------------------
  # Squeezing with #squeeze 0.15 |
  # with #gsub 0.37 |
  # ---------------------------------
  # Spliting with #split 0.43 |
  # with #match 0.29 |
  # ---------------------------------
  
  Benchmark.warmer(TIMES) do
    report "squeezing with #squeeze" do
      "abc//def//ghi//jkl".squeeze("/")
    end
    report "squeezing with #gsub" do
      "abc//def//ghi//jkl".gsub(/\/+/, "/")
    end
  end
  
  # Output:
  # Running the benchmarks 100000 times each...
  #
  # Results |
  # ----------------------------------
  # squeezing with #squeeze 0.15 |
  # squeezing with #gsub 0.34 |
  # ----------------------------------