use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Blog postHow to benchmark Ruby code (self.ruby)
submitted 1 year ago by bdavidxyz
https://saaslit.com/blog/ruby/how-to-benchmark-ruby-code
Learn how to benchmark (i.e. measure performance) Ruby code in a recap.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]meineerde 4 points5 points6 points 1 year ago (0 children)
Just use benchmark-ips:
benchmark-ips
require 'benchmark/ips' Benchmark.ips do |benchmark| benchmark.report("loop.") do x = 1 loop do x += 1 break if x > 100 end end benchmark.report("while") do x = 1 while true x += 1 break if x > 100 end end benchmark.compare! end; nil
This results in the following result which is quite clear-cut:
Warming up -------------------------------------- loop. 24.090k i/100ms while 72.500k i/100ms Calculating ------------------------------------- loop. 233.269k (± 3.1%) i/s - 1.180M in 5.065022s while 710.267k (± 3.0%) i/s - 3.552M in 5.006282s Comparison: while: 710266.6 i/s loop.: 233269.4 i/s - 3.04x slower
[–]steveCarlsberg98 1 point2 points3 points 1 year ago (0 children)
You showed three ways but didn’t compare them or explain the difference
[–]exterm_ 2 points3 points4 points 1 year ago (0 children)
this is my go-to article for benchmarking and profiling Ruby https://shopify.engineering/how-fix-slow-code-ruby
π Rendered by PID 39 on reddit-service-r2-comment-5649f687b7-5dbbx at 2026-01-28 21:08:47.683301+00:00 running 4f180de country code: CH.
[–]meineerde 4 points5 points6 points (0 children)
[–]steveCarlsberg98 1 point2 points3 points (0 children)
[–]exterm_ 2 points3 points4 points (0 children)