Performance

Benchmark stdlib

Guesses about Ruby speed are often wrong, so measure with the benchmark library. The bm helper runs each report block and prints timings.

Compare two approaches side by side and keep the faster one only when the difference survives repeated runs.

require "benchmark" Benchmark.bm do |x| x.report("plus:") { 1000.times { "a" + "b" } } end

Magic frozen_string_literal comment

Every string literal normally allocates a fresh mutable object. The magic comment, placed before any code in the file, freezes all literals of that file instead.

Frozen strings can be shared safely, which trims both allocation count and garbage collector pressure.

# frozen_string_literal: true name = "Ada" puts name.frozen?

ruby frozen.rb

true

Avoid garbage

Intermediate arrays and strings cost time twice, once to build and once to collect. Bang methods transform in place and skip the throwaway copy.

Prefer map with a bang over plain map when the original array is no longer needed elsewhere.

names = ["ada", "bob"] names.map! { |n| n.upcase } puts names.inspect

ruby alloc.rb

["ADA", "BOB"]

Measure first

Optimize the hot path, not a random line. A monotonic clock around the suspect code shows elapsed seconds without wall clock jumps.

When the number refuses to shrink, the bottleneck lives elsewhere, so profile a wider scope before rewriting more code.

start = Process.clock_gettime(Process::CLOCK_MONOTONIC) 1000.times { "x" * 10 } done = Process.clock_gettime(Process::CLOCK_MONOTONIC) puts(done - start)

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does the frozen_string_literal comment have to be?

    Answer

    Before any code in the file, so a shebang or an encoding line may come first

    The comment is read before the program runs, and a line that is not code may stand above it.

  2. What does this program print? # frozen_string_literal: true name = "Ada" puts name.frozen?

    Answer

    true

    Every literal of the file is frozen, so the string answers true and cannot be changed in place.

  3. What does this program print? names = ["ada", "bob"] names.map! { |n| n.upcase } p names

    Answer

    ["ADA", "BOB"]

    The bang method writes the new strings back into the same array, which is the copy the page avoids.

  4. What does this program print? start = Process.clock_gettime(Process::CLOCK_MONOTONIC) done = Process.clock_gettime(Process::CLOCK_MONOTONIC) p (done - start) >= 0

    Answer

    true

    The monotonic clock never goes backwards, so the difference of two readings is at least zero.

  5. Why does an intermediate string cost twice?

    Answer

    It is built once and collected once

    The allocation is the first cost and the garbage collection the second, which is why avoiding the copy helps.

  6. What does this program print? names = ["a"] copy = names.map(&:upcase) names.map!(&:upcase) p copy.equal?(names)

    Answer

    false

    map builds a new array and map! writes into the old one, so the two names refer to different objects.

  7. When is map! the right choice over map?

    Answer

    When the original array is no longer needed elsewhere

    The bang form saves the copy, and it changes the caller's array, which is a problem when someone else still holds it.

  8. What does this program print? p ("x" * 10).size

    Answer

    10

    The repeated literal is ten characters long, and the allocation of it is the work the profile measures.

  9. What does a monotonic clock protect a measurement from?

    Answer

    The wall clock jumping forward or back during the run

    The monotonic reading only moves forward, so an NTP correction cannot make the elapsed time negative.

  10. What does this program print? puts ["ADA", "BOB"].inspect

    Answer

    ["ADA", "BOB"]

    inspect writes the array the way a terminal shows it, and puts prints that text without extra quotes.

Article author: Arthur Isaev