Profiling Tools
ruby-prof and stackprof.
Profiling Tools is a free Ruby Academy lesson on CoddyKit — lesson 2 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.
Benchmark vs Profiler
A benchmark tells you how long something takes. A profiler tells you where the time goes inside it.
- Profilers break time down per method or line
- They reveal hotspots you would never guess
- Popular Ruby profilers: ruby-prof and stackprof
# Profilers answer: which method consumed the most time?
puts 'Benchmark = how long, Profiler = where'ruby-prof Overview
ruby-prof is a detailed instrumenting profiler. It hooks into every method call.
- Very precise call counts and timings
- Higher overhead, so use on representative samples
- Installed with
gem install ruby-prof
# Gemfile
gem 'ruby-prof'Profiling a Block with ruby-prof
Wrap code in RubyProf.profile to capture a result you can print.
- Returns a profile result object
- Feed it to a printer for output
require 'ruby-prof'
result = RubyProf.profile do
1000.times { |i| (1..i).reduce(:+) }
end
RubyProf::FlatPrinter.new(result).print(STDOUT)ruby-prof Printers
ruby-prof offers several printers for different views.
FlatPrintera sorted list of methodsGraphPrintercaller/callee relationshipsCallTreePrinteroutput for KCachegrind
require 'ruby-prof'
result = RubyProf.profile { 100.times { 'x' * 1000 } }
RubyProf::GraphPrinter.new(result).print(STDOUT, min_percent: 1)Reading a Flat Profile
A flat profile lists methods by time consumed.
- %self time spent in the method itself
- total time including callees
- calls how many times invoked
Focus on the highest %self entries first.
# Example flat output columns:
# %self total self wait child calls name
puts 'Optimize the top %self method first'stackprof Overview
stackprof is a sampling profiler. It periodically records the call stack.
- Very low overhead, safe for production
- Samples by CPU, wall, or object allocation
- Installed with
gem install stackprof
# Gemfile
gem 'stackprof'Running stackprof
StackProf.run profiles a block and writes results to a file.
mode:can be:cpu,:wall, or:objectout:sets the dump file path
require 'stackprof'
StackProf.run(mode: :cpu, out: 'profile.dump') do
1_000_000.times { |i| Math.sqrt(i) }
endInspecting stackprof Results
Use the stackprof command line tool to read the dump.
stackprof profile.dump --textfor a summary--method Name#methdrills into one method--flamegraphproduces flamegraph data
# Shell
# stackprof profile.dump --text
# stackprof profile.dump --method 'Array#sort'
puts 'Analyze the dump with the stackprof CLI'Sampling vs Instrumenting
The two profilers trade accuracy for overhead.
- ruby-prof instruments every call: exact but slower
- stackprof samples: approximate but cheap
- Use stackprof in production, ruby-prof for deep dives
modes = { 'ruby-prof' => 'instrumenting', 'stackprof' => 'sampling' }
modes.each { |tool, kind| puts "#{tool}: #{kind}" }Wall vs CPU Time
Choose the profiling mode based on the bottleneck.
- CPU mode shows compute heavy code
- Wall mode also captures I/O waits like network or disk
- Slow endpoints with DB calls need wall mode
# I/O bound? use wall. CPU bound? use cpu.
bottleneck = 'database query'
mode = bottleneck.include?('query') ? :wall : :cpu
puts "Use mode: #{mode}"A Profiling Workflow
Effective profiling follows a loop.
- Reproduce the slow scenario
- Profile to find the hotspot
- Optimize only that hotspot
- Re-profile to confirm improvement
steps = ['reproduce', 'profile', 'optimize hotspot', 're-profile']
steps.each_with_index { |s, i| puts "#{i + 1}. #{s}" }Quick Check
Test your profiling knowledge.
Recap
You learned about profiling tools:
- Profilers show where time is spent, not just how long
ruby-profinstruments every call for precise detailstackprofsamples cheaply, safe for production- Choose CPU vs wall mode based on the bottleneck
- Follow the reproduce, profile, optimize, re-profile loop
Next you will reduce memory allocations.
Frequently asked questions
Is the “Profiling Tools” lesson free?
Yes — the full text of “Profiling Tools” 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 “Profiling Tools”?
ruby-prof and stackprof. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Profiling Tools” 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
- Measuring Performance
- Profiling Tools
- Memory Optimization
- Common Bottlenecks