radius-spec 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Run from the command line: bundle exec ruby benchmarks/gsub_vs_tr.rb
4
+ require_relative 'bm_setup'
5
+
6
+ display_benchmark_header
7
+
8
+ [
9
+ ["No Replacement", "Any String", "m", /m/, "-"],
10
+ ["Single Replacement", "Any String", "i", /i/, "-"],
11
+ ["Multiple Replacement", "Any String", "n", /n/, "-"],
12
+ ].each do |title, str, pattern, regexp, replacement|
13
+ section title do |bench|
14
+ bench.report("gsub(string)") do
15
+ str.gsub(pattern, replacement)
16
+ end
17
+
18
+ bench.report("gsub(regexp)") do
19
+ str.gsub(regexp, replacement)
20
+ end
21
+
22
+ bench.report("tr") do
23
+ str.tr(pattern, replacement)
24
+ end
25
+ end
26
+ end
27
+
28
+ __END__
29
+
30
+ ### Environment
31
+
32
+ ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
33
+ GC Disabled: false
34
+
35
+ ### Test Cases
36
+
37
+ #### No Replacement
38
+
39
+ ```
40
+ Warming up --------------------------------------
41
+ gsub(string) 225.903k i/100ms
42
+ gsub(regexp) 150.469k i/100ms
43
+ tr 225.874k i/100ms
44
+ Calculating -------------------------------------
45
+ gsub(string) 4.848M (± 0.7%) i/s - 24.398M in 5.042073s
46
+ gsub(regexp) 2.558M (± 1.3%) i/s - 12.790M in 5.018370s
47
+ tr 4.872M (± 1.0%) i/s - 24.394M in 5.020983s
48
+ with 95.0% confidence
49
+
50
+ Comparison:
51
+ tr: 4872264.7 i/s
52
+ gsub(string): 4847903.4 i/s - same-ish: difference falls within error
53
+ gsub(regexp): 2557821.1 i/s - 1.91x (± 0.03) slower
54
+ with 95.0% confidence
55
+ ```
56
+
57
+ #### Single Replacement
58
+
59
+ ```
60
+ Warming up --------------------------------------
61
+ gsub(string) 115.202k i/100ms
62
+ gsub(regexp) 64.609k i/100ms
63
+ tr 230.643k i/100ms
64
+ Calculating -------------------------------------
65
+ gsub(string) 1.431M (± 1.6%) i/s - 7.143M in 5.010792s
66
+ gsub(regexp) 663.965k (± 1.0%) i/s - 3.360M in 5.067248s
67
+ tr 4.517M (± 0.9%) i/s - 22.603M in 5.014505s
68
+ with 95.0% confidence
69
+
70
+ Comparison:
71
+ tr: 4516906.0 i/s
72
+ gsub(string): 1430926.3 i/s - 3.16x (± 0.06) slower
73
+ gsub(regexp): 663964.7 i/s - 6.80x (± 0.09) slower
74
+ with 95.0% confidence
75
+ ```
76
+
77
+ #### Multiple Replacement
78
+
79
+ ```
80
+ Warming up --------------------------------------
81
+ gsub(string) 92.833k i/100ms
82
+ gsub(regexp) 51.482k i/100ms
83
+ tr 222.779k i/100ms
84
+ Calculating -------------------------------------
85
+ gsub(string) 1.263M (± 1.3%) i/s - 6.313M in 5.014495s
86
+ gsub(regexp) 663.557k (± 1.4%) i/s - 3.346M in 5.061408s
87
+ tr 4.786M (± 0.9%) i/s - 24.060M in 5.040074s
88
+ with 95.0% confidence
89
+
90
+ Comparison:
91
+ tr: 4785813.8 i/s
92
+ gsub(string): 1262609.0 i/s - 3.79x (± 0.06) slower
93
+ gsub(regexp): 663557.0 i/s - 7.21x (± 0.12) slower
94
+ with 95.0% confidence
95
+ ```
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Run from the command line: bundle exec ruby benchmarks/hash_merge.rb
4
+ require_relative 'bm_setup'
5
+
6
+ display_benchmark_header
7
+
8
+ ENUM = (1..100)
9
+
10
+ def hash_merge
11
+ tmp = {}
12
+ ENUM.each do |e|
13
+ tmp.merge! e => e
14
+ end
15
+ tmp
16
+ end
17
+
18
+ def hash_assign
19
+ tmp = {}
20
+ ENUM.each do |e|
21
+ tmp[e] = e
22
+ end
23
+ tmp
24
+ end
25
+
26
+ COMPOSE = {
27
+ a: 'a',
28
+ b: 'b',
29
+ c: 'c',
30
+ }.freeze
31
+
32
+ def hash_compose_merge
33
+ { one: 1 }.merge(COMPOSE)
34
+ end
35
+
36
+ def hash_compose_merge!
37
+ { one: 1 }.merge!(COMPOSE)
38
+ end
39
+
40
+ def hash_compose_splat
41
+ { one: 1, **COMPOSE }
42
+ end
43
+
44
+ section "Hash merge vs assign" do |bench|
45
+ bench.report("Hash#merge!") do
46
+ hash_merge
47
+ end
48
+
49
+ bench.report("Hash#[]=") do
50
+ hash_assign
51
+ end
52
+ end
53
+
54
+ section "Hash compose: merge vs splat" do |bench|
55
+ bench.report("merge") do
56
+ hash_compose_merge
57
+ end
58
+
59
+ bench.report("merge!") do
60
+ hash_compose_merge!
61
+ end
62
+
63
+ bench.report("splat") do
64
+ hash_compose_splat
65
+ end
66
+ end
67
+
68
+ __END__
69
+
70
+ ### Environment
71
+
72
+ ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
73
+ GC Disabled: false
74
+
75
+ ### Test Cases
76
+
77
+ #### Hash merge vs assign
78
+
79
+ ```
80
+ Warming up --------------------------------------
81
+ Hash#merge! 1.760k i/100ms
82
+ Hash#[]= 7.821k i/100ms
83
+ Calculating -------------------------------------
84
+ Hash#merge! 17.746k (± 1.1%) i/s - 89.760k in 5.065762s
85
+ Hash#[]= 83.691k (± 1.2%) i/s - 422.334k in 5.057697s
86
+ with 95.0% confidence
87
+
88
+ Comparison:
89
+ Hash#[]=: 83691.2 i/s
90
+ Hash#merge!: 17746.3 i/s - 4.71x (± 0.08) slower
91
+ with 95.0% confidence
92
+ ```
93
+
94
+ #### Hash compose: merge vs splat
95
+
96
+ ```
97
+ Warming up --------------------------------------
98
+ merge 78.448k i/100ms
99
+ merge! 111.435k i/100ms
100
+ splat 111.927k i/100ms
101
+ Calculating -------------------------------------
102
+ merge 978.337k (± 1.0%) i/s - 4.942M in 5.061428s
103
+ merge! 1.571M (± 1.0%) i/s - 7.912M in 5.046875s
104
+ splat 1.587M (± 1.0%) i/s - 7.947M in 5.017037s
105
+ with 95.0% confidence
106
+
107
+ Comparison:
108
+ splat: 1587073.4 i/s
109
+ merge!: 1570636.9 i/s - same-ish: difference falls within error
110
+ merge: 978337.3 i/s - 1.62x (± 0.02) slower
111
+ with 95.0% confidence
112
+ ```
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Run from the command line: bundle exec ruby benchmarks/kwargs.rb
4
+ require_relative 'bm_setup'
5
+
6
+ display_benchmark_header
7
+
8
+ NAME = "any name"
9
+ STATIC_OPTS = { name: "any name" }.freeze
10
+
11
+ def hash_param(hash)
12
+ hash
13
+ end
14
+
15
+ def kwarg_param(name: nil)
16
+ name
17
+ end
18
+
19
+ def kwarg_splat_param(**kwargs)
20
+ kwargs
21
+ end
22
+
23
+ def static_opts
24
+ kwarg_param STATIC_OPTS
25
+ end
26
+
27
+ def named_opts
28
+ kwarg_param name: NAME
29
+ end
30
+
31
+ def splat_opts
32
+ kwarg_splat_param name: NAME
33
+ end
34
+
35
+ def hash_opts
36
+ hash_param name: NAME
37
+ end
38
+
39
+ section "kwargs vs hash method" do |x|
40
+ x.report("hash param") do
41
+ hash_opts
42
+ end
43
+
44
+ x.report("kwarg param") do
45
+ named_opts
46
+ end
47
+
48
+ x.report("kwarg splat param") do
49
+ splat_opts
50
+ end
51
+
52
+ x.compare!
53
+ end
54
+
55
+ section "Call kwargs method" do |x|
56
+ x.report("static opts") do
57
+ static_opts
58
+ end
59
+
60
+ x.report("named opts") do
61
+ named_opts
62
+ end
63
+
64
+ x.compare!
65
+ end
66
+
67
+ __END__
68
+
69
+ Perhaps surprisingly the kwarg parameters option is much faster. I'm not sure
70
+ why this actually is, but my guess is that Ruby optimizes this during the byte
71
+ code process.
72
+
73
+ ### Environment
74
+
75
+ ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
76
+ GC Disabled: false
77
+
78
+ ### Test Cases
79
+
80
+ #### kwargs vs hash method
81
+
82
+ ```
83
+ Warming up --------------------------------------
84
+ hash param 131.753k i/100ms
85
+ kwarg param 266.657k i/100ms
86
+ kwarg splat param 66.641k i/100ms
87
+ Calculating -------------------------------------
88
+ hash param 2.057M (± 1.6%) i/s - 10.277M in 5.023252s
89
+ kwarg param 7.351M (± 0.7%) i/s - 36.799M in 5.015356s
90
+ kwarg splat param 850.179k (± 1.4%) i/s - 4.265M in 5.035194s
91
+ with 95.0% confidence
92
+
93
+ Comparison:
94
+ kwarg param: 7351007.8 i/s
95
+ hash param: 2056501.2 i/s - 3.57x (± 0.06) slower
96
+ kwarg splat param: 850179.4 i/s - 8.64x (± 0.13) slower
97
+ with 95.0% confidence
98
+ ```
99
+
100
+ #### Call kwargs method
101
+
102
+ ```
103
+ Warming up --------------------------------------
104
+ static opts 127.699k i/100ms
105
+ named opts 274.978k i/100ms
106
+ Calculating -------------------------------------
107
+ static opts 1.879M (± 1.0%) i/s - 9.450M in 5.039627s
108
+ named opts 6.938M (± 1.1%) i/s - 34.647M in 5.014500s
109
+ with 95.0% confidence
110
+
111
+ Comparison:
112
+ named opts: 6937888.3 i/s
113
+ static opts: 1878705.6 i/s - 3.69x (± 0.05) slower
114
+ with 95.0% confidence
115
+ ```
116
+
117
+ ### Environment
118
+
119
+ ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
120
+ GC Disabled: true
121
+
122
+ ### Test Cases
123
+
124
+ #### kwargs vs hash method
125
+
126
+ ```
127
+ Warming up --------------------------------------
128
+ hash param 144.722k i/100ms
129
+ kwarg param 266.216k i/100ms
130
+ kwarg splat param 67.212k i/100ms
131
+ Calculating -------------------------------------
132
+ hash param 2.318M (± 5.2%) i/s - 10.709M in 5.017846s
133
+ kwarg param 7.381M (± 0.8%) i/s - 37.004M in 5.026198s
134
+ kwarg splat param 893.963k (±15.1%) i/s - 3.159M in 5.180853s
135
+ with 95.0% confidence
136
+
137
+ Comparison:
138
+ kwarg param: 7380700.1 i/s
139
+ hash param: 2317626.6 i/s - 3.19x (± 0.17) slower
140
+ kwarg splat param: 893963.2 i/s - 8.26x (± 1.26) slower
141
+ with 95.0% confidence
142
+ ```
143
+
144
+ #### Call kwargs method
145
+
146
+ ```
147
+ Warming up --------------------------------------
148
+ static opts 157.568k i/100ms
149
+ named opts 271.741k i/100ms
150
+ Calculating -------------------------------------
151
+ static opts 1.741M (±12.7%) i/s - 7.091M in 5.038616s
152
+ named opts 7.335M (± 0.7%) i/s - 36.685M in 5.010800s
153
+ with 95.0% confidence
154
+
155
+ Comparison:
156
+ named opts: 7335402.3 i/s
157
+ static opts: 1740760.5 i/s - 4.21x (± 0.53) slower
158
+ with 95.0% confidence
159
+ ```
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Run from the command line: bundle exec ruby benchmarks/max_ternary.rb
4
+ require_relative 'bm_setup'
5
+
6
+ display_benchmark_header
7
+
8
+ section "Min First" do |bench|
9
+ x = 1
10
+ y = 2
11
+
12
+ bench.report("max") do
13
+ [x, y].max
14
+ end
15
+
16
+ bench.report("ternary") do
17
+ (x < y) ? y : x
18
+ end
19
+ end
20
+
21
+ section "Max First" do |bench|
22
+ x = 2
23
+ y = 1
24
+
25
+ bench.report("max") do
26
+ [x, y].max
27
+ end
28
+
29
+ bench.report("ternary") do
30
+ (x < y) ? y : x
31
+ end
32
+ end
33
+
34
+ section "Equal values" do
35
+ x = 2
36
+ y = 2
37
+
38
+ bench.report("max") do
39
+ [x, y].max
40
+ end
41
+
42
+ bench.report("ternary") do
43
+ (x < y) ? y : x
44
+ end
45
+ end
46
+
47
+ __END__
48
+
49
+ ### Environment
50
+
51
+ ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
52
+ GC Disabled: false
53
+
54
+ ### Test Cases
55
+
56
+ #### Min First
57
+
58
+ ```
59
+ Warming up --------------------------------------
60
+ max 349.454k i/100ms
61
+ ternary 349.128k i/100ms
62
+ Calculating -------------------------------------
63
+ max 12.628M (± 0.9%) i/s - 62.902M in 5.000232s
64
+ ternary 12.853M (± 0.8%) i/s - 64.240M in 5.015540s
65
+ with 95.0% confidence
66
+
67
+ Comparison:
68
+ ternary: 12852607.6 i/s
69
+ max: 12628052.1 i/s - 1.02x (± 0.01) slower
70
+ with 95.0% confidence
71
+ ```
72
+
73
+ #### Max First
74
+
75
+ ```
76
+ Warming up --------------------------------------
77
+ max 346.104k i/100ms
78
+ ternary 344.003k i/100ms
79
+ Calculating -------------------------------------
80
+ max 12.788M (± 0.7%) i/s - 64.029M in 5.022073s
81
+ ternary 12.607M (± 0.7%) i/s - 62.953M in 5.006764s
82
+ with 95.0% confidence
83
+
84
+ Comparison:
85
+ max: 12787891.5 i/s
86
+ ternary: 12606659.1 i/s - same-ish: difference falls within error
87
+ with 95.0% confidence
88
+ ```
89
+
90
+ #### Equal values
91
+
92
+ ```
93
+ Warming up --------------------------------------
94
+ max 340.468k i/100ms
95
+ ternary 343.788k i/100ms
96
+ Calculating -------------------------------------
97
+ max 12.278M (± 0.9%) i/s - 61.284M in 5.010572s
98
+ ternary 12.703M (± 0.9%) i/s - 63.601M in 5.026552s
99
+ with 95.0% confidence
100
+
101
+ Comparison:
102
+ ternary: 12702775.1 i/s
103
+ max: 12277915.1 i/s - 1.03x (± 0.01) slower
104
+ with 95.0% confidence
105
+ ```