Skip to content

Commit

Permalink
Add dumb loop microbenchmark (#349)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
maximecb authored Dec 3, 2024
1 parent 50a36ba commit 1dbbdce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,7 @@ ruby-xor:
desc: pure-Ruby string XOR microbenchmark, analogous to xorcist C extension.
category: micro
single_file: true
loops-times:
desc: nested loop Integer#times and array access microbenchmark
category: micro
single_file: true
21 changes: 21 additions & 0 deletions benchmarks/loops-times.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../harness/loader'

# Fix these values for determinism
u = 5
r = 7

run_benchmark(10) do
a = Array.new(10000, 0)

4_000.times do |i|
4_000.times do |j|
a[i] += j % u
end
a[i] += r
end

result = a[r]
if result != 8007
raise "incorrect result"
end
end

0 comments on commit 1dbbdce

Please sign in to comment.