Skip to content

Commit 4fc54ac

Browse files
committed
Make the methods polymorphic
1 parent a5d4ae8 commit 4fc54ac

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎benchmarks.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ attr_accessor:
171171
category: micro
172172
single_file: true
173173
ractor: true
174+
block_methods:
175+
desc: calls a handful of builtin Ruby methods that take blocks.
176+
category: micro
177+
single_file: true
174178
cfunc_itself:
175179
desc: cfunc_itself just calls the 'itself' method many, many times.
176180
category: micro

‎benchmarks/block_methods.rb‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require_relative '../harness/loader'
2+
3+
def array_each(array)
4+
array.each do |x|
5+
x * 2
6+
end
7+
array.each do |x|
8+
x * 4
9+
end
10+
end
11+
12+
def array_map(array)
13+
array.map do |x|
14+
x * 2
15+
end
16+
array.map do |x|
17+
x * 4
18+
end
19+
end
20+
21+
def array_select(array)
22+
array.select do |x|
23+
x.even?
24+
end
25+
array.select do |x|
26+
x.odd?
27+
end
28+
end
29+
30+
def integer_times(integer)
31+
integer.times do |i|
32+
i * 2
33+
end
34+
integer.times do |i|
35+
i * 4
36+
end
37+
end
38+
39+
def kernel_tap(x)
40+
i = 0
41+
while i < x
42+
x.tap do |y|
43+
y * 2
44+
end
45+
x.tap do |y|
46+
y * 4
47+
end
48+
i += 1
49+
end
50+
end
51+
52+
def kernel_then(x)
53+
i = 0
54+
while i < x
55+
x.then do |y|
56+
y * 2
57+
end
58+
x.then do |y|
59+
y * 4
60+
end
61+
i += 1
62+
end
63+
end
64+
65+
K = 100_000
66+
ARRAY = (1..K).to_a
67+
68+
run_benchmark(1000) do
69+
i = 0
70+
while i < 10
71+
array_each(ARRAY)
72+
array_map(ARRAY)
73+
array_select(ARRAY)
74+
integer_times(K)
75+
kernel_tap(K)
76+
kernel_then(K)
77+
i += 1
78+
end
79+
end

0 commit comments

Comments
 (0)