Simple Ruby Benchmark

09-Jan-2021 1825

Just wanted to check results of running different ruby versions managed by rbenv.

Sample script used from https://ruby-doc.org/stdlib-2.5.0/libdoc/benchmark/rdoc/Benchmark.html

require 'benchmark'
include Benchmark         # we need the CAPTION and FORMAT constants

n = 10000000
Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
  tf = x.report("for:")   { for i in 1..n; a = "1"; end }
  tt = x.report("times:") { n.times do   ; a = "1"; end }
  tu = x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
  [tf+tt+tu, (tf+tt+tu)/3]
end

Laptop: Dell XPS 15" old 2 years

OS: Ubuntu 20.04 updated

Ruby versions /  Results 

2.2.1    = 0.700070
2.3.0    = 0.511516
2.4.2    = 0.455849
2.5.0    = 0.554192
2.6.4    = 0.489311
2.7.0    = 0.657698
2.7.1    = 0.556575
2.7.2    = 0.556223
3.0.0    = 0.642555

Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle