Performance

Contents
Benchmark stdlib
Magic frozen_string_literal comment
Avoid garbage
Measure first

Benchmark stdlib

Guesses about 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 at the very first line freezes all literals in the 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?

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

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)

Next: Modern Ruby tour

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: