0Pricing
Ruby Academy · Lesson

Measuring Performance

Benchmark module.

Measuring Performance is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Measure?

Optimizing without measuring is guessing. The Benchmark module in Ruby's standard library times code precisely.

  • Find the slow parts before changing them
  • Compare implementations objectively
  • Avoid premature optimization
require 'benchmark'

time = Benchmark.realtime do
  1_000_000.times { |i| i * 2 }
end
puts "Took #{time.round(4)}s"

Benchmark.measure

Benchmark.measure returns a report of user, system, and real time.

  • User time is CPU in your code
  • Real time is wall clock elapsed
require 'benchmark'

result = Benchmark.measure do
  500_000.times { |i| i.to_s }
end
puts result

Benchmark.bm

Benchmark.bm runs and labels multiple blocks in a table.

  • Pass a label width for alignment
  • Each x.report is one row
require 'benchmark'

Benchmark.bm(10) do |x|
  x.report('to_s:') { 100_000.times { |i| i.to_s } }
  x.report('inspect:') { 100_000.times { |i| i.inspect } }
end

Benchmark.bmbm

Benchmark.bmbm runs a rehearsal first to warm caches and the GC, giving fairer numbers.

  • The first pass is discarded
  • The second pass is the real measurement
require 'benchmark'

Benchmark.bmbm do |x|
  x.report('sum:') { (1..100_000).sum }
  x.report('reduce:') { (1..100_000).reduce(:+) }
end

realtime for Quick Timing

Benchmark.realtime returns just the wall clock seconds as a float.

  • Simplest way to time one block
  • Good for ad hoc checks
require 'benchmark'

elapsed = Benchmark.realtime do
  arr = Array.new(100_000) { rand }
  arr.sort
end
puts "Sorted in #{elapsed.round(4)}s"

Monotonic Clock

For precise timing not affected by system clock changes, use Process.clock_gettime with a monotonic clock.

  • Immune to NTP adjustments
  • The most accurate built-in option
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
200_000.times { |i| i * i }
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts "Elapsed: #{(finish - start).round(4)}s"

Comparing Two Approaches

Benchmarks shine when comparing alternatives. Run both under identical load.

  • Keep input size large enough to be measurable
  • Repeat to reduce noise
require 'benchmark'

arr = (1..100_000).to_a
Benchmark.bm(8) do |x|
  x.report('map:') { arr.map { |n| n * 2 } }
  x.report('each:') { r = []; arr.each { |n| r << n * 2 } }
end

Beware Microbenchmarks

Tiny benchmarks can mislead.

  • JIT and caches may not reflect real workloads
  • The GC can fire mid measurement
  • Measure realistic data sizes
require 'benchmark'

# Too small to be meaningful
t = Benchmark.realtime { 10.times { |i| i + 1 } }
puts "Misleadingly tiny: #{t}s"

Iterations Per Second

Often you want throughput: how many operations per second. The benchmark-ips gem reports this, but you can approximate it manually.

  • Count iterations in a fixed time window
  • Higher ips means faster
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 0.2
count = 0
while Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
  count += 1
  'hello'.upcase
end
puts "Iterations in 0.2s: #{count}"

Controlling the GC

The garbage collector can skew results. You can disable it temporarily for steadier numbers.

  • GC.disable before, GC.enable after
  • Use cautiously to avoid memory growth
require 'benchmark'

GC.disable
t = Benchmark.realtime { 100_000.times { |i| i.to_s } }
GC.enable
puts "Without GC: #{t.round(4)}s"

Averaging Multiple Runs

Run a benchmark several times and average to smooth out variance.

  • Discard the slowest outlier if needed
  • Report the median for stability
require 'benchmark'

times = 3.times.map do
  Benchmark.realtime { (1..50_000).map { |n| n * n } }
end
avg = times.sum / times.size
puts "Average: #{avg.round(4)}s"

Quick Check

Test your benchmarking knowledge.

Recap

You learned to measure performance:

  • The Benchmark module times code accurately
  • realtime, measure, bm, and bmbm serve different needs
  • Use the monotonic clock for precise timing
  • Avoid misleading microbenchmarks and account for the GC
  • Average multiple runs to reduce noise

Next you will use dedicated profiling tools.

Frequently asked questions

Is the “Measuring Performance” lesson free?

Yes — the full text of “Measuring Performance” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Measuring Performance”?

Benchmark module. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Measuring Performance” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Measuring Performance
  2. Profiling Tools
  3. Memory Optimization
  4. Common Bottlenecks
← Back to Ruby Academy