Skip to content

Commit 1dbbdce

Browse files
authored
Add dumb loop microbenchmark (#349)
This is a modified version of a microbenchmark circulated on twitter by "TheDumbTechGuy". It's obviously not representative of most Ruby code, but it does show areas where we could perform much better with better inlining and better quality code generation.
1 parent 50a36ba commit 1dbbdce

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

benchmarks.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,7 @@ ruby-xor:
147147
desc: pure-Ruby string XOR microbenchmark, analogous to xorcist C extension.
148148
category: micro
149149
single_file: true
150+
loops-times:
151+
desc: nested loop Integer#times and array access microbenchmark
152+
category: micro
153+
single_file: true

benchmarks/loops-times.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require_relative '../harness/loader'
2+
3+
# Fix these values for determinism
4+
u = 5
5+
r = 7
6+
7+
run_benchmark(10) do
8+
a = Array.new(10000, 0)
9+
10+
4_000.times do |i|
11+
4_000.times do |j|
12+
a[i] += j % u
13+
end
14+
a[i] += r
15+
end
16+
17+
result = a[r]
18+
if result != 8007
19+
raise "incorrect result"
20+
end
21+
end

0 commit comments

Comments
 (0)