From 8bfee9473a3796ed1fb4fbc2a06f566dd6a11e81 Mon Sep 17 00:00:00 2001 From: Maxime Chevalier-Boisvert Date: Tue, 3 Dec 2024 15:26:02 -0500 Subject: [PATCH] Add dumb loop microbenchmark 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. --- benchmarks.yml | 4 ++++ benchmarks/loops-times.rb | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 benchmarks/loops-times.rb diff --git a/benchmarks.yml b/benchmarks.yml index 038bf2d8..65465a90 100644 --- a/benchmarks.yml +++ b/benchmarks.yml @@ -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 diff --git a/benchmarks/loops-times.rb b/benchmarks/loops-times.rb new file mode 100644 index 00000000..485fa891 --- /dev/null +++ b/benchmarks/loops-times.rb @@ -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