Bird
Raised Fist0
Rubyprogramming~20 mins

Performance profiling basics in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Performance Profiling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Ruby code using Benchmark.measure
What is the output type of the following Ruby code snippet using Benchmark.measure?
Ruby
require 'benchmark'
result = Benchmark.measure do
  sum = 0
  (1..1000).each { |i| sum += i }
end
puts result.class
ABenchmark::Tms
BString
CFloat
DInteger
Attempts:
2 left
💡 Hint
Benchmark.measure returns an object that holds timing data, not a simple number or string.
❓ Predict Output
intermediate
2:00remaining
Output of Ruby code using GC.stat
What is the output type of the following Ruby code snippet?
Ruby
stats = GC.stat
puts stats.class
AHash
BArray
CString
DInteger
Attempts:
2 left
💡 Hint
GC.stat returns a collection of statistics about garbage collection.
❓ Predict Output
advanced
2:00remaining
Output of Ruby code using Benchmark.bm
What will be printed by this Ruby code snippet?
Ruby
require 'benchmark'
Benchmark.bm do |x|
  x.report("sleep") { sleep(0.1) }
end
AA string 'sleep 0.1' printed
BAn error because Benchmark.bm requires a block with arguments
CNothing, the code runs silently
DA table with a header and one row showing label 'sleep' and times around 0.1 seconds
Attempts:
2 left
💡 Hint
Benchmark.bm prints a formatted table with timing results for each report block.
🧠 Conceptual
advanced
2:00remaining
Identifying the cause of slow Ruby code
You run a Ruby program and notice it is slow. Which tool or method is best to find which part of the code takes the most time?
AUse puts statements to print variable values
BUse GC.start to force garbage collection
CUse Benchmark.measure blocks around suspected code sections
DUse sleep to slow down the program
Attempts:
2 left
💡 Hint
Profiling tools measure time spent in code sections.
❓ Predict Output
expert
2:00remaining
Output of Ruby code using GC.stat keys
What is the output of this Ruby code snippet?
Ruby
stats = GC.stat
puts stats.keys.include?(:total_allocated_objects)
ARaises KeyError
Btrue
Cnil
Dfalse
Attempts:
2 left
💡 Hint
Check if :total_allocated_objects is a valid key in GC.stat hash.

Practice

(1/5)
1. What is the main purpose of performance profiling in Ruby?
easy
A. To find slow parts of the code that need optimization
B. To add new features to the Ruby program
C. To check syntax errors in the code
D. To convert Ruby code into another language

Solution

  1. Step 1: Understand the goal of profiling

    Performance profiling is used to measure how long parts of code take to run.
  2. Step 2: Identify the main use

    It helps find slow parts so you can improve them.
  3. Final Answer:

    To find slow parts of the code that need optimization -> Option A
  4. Quick Check:

    Profiling = Find slow code [OK]
Hint: Profiling = finding slow code parts to speed up [OK]
Common Mistakes:
  • Confusing profiling with debugging syntax errors
  • Thinking profiling adds features
  • Believing profiling translates code
2. Which Ruby gem is commonly used for performance profiling?
easy
A. nokogiri
B. rails
C. ruby-prof
D. rspec

Solution

  1. Step 1: Recall common Ruby gems

    Rails is a web framework, Nokogiri is for XML/HTML parsing, RSpec is for testing.
  2. Step 2: Identify profiling gem

    ruby-prof is the gem designed for performance profiling.
  3. Final Answer:

    ruby-prof -> Option C
  4. Quick Check:

    Profiling gem = ruby-prof [OK]
Hint: ruby-prof gem is for profiling Ruby code [OK]
Common Mistakes:
  • Choosing Rails as a profiling tool
  • Confusing testing gem RSpec with profiling
  • Picking unrelated gems like Nokogiri
3. Given this Ruby profiling output snippet:
Method       | Total Time | Self Time
calculate    | 0.50       | 0.40
process      | 0.30       | 0.10
render       | 0.20       | 0.20
Which method should you optimize first to improve performance?
medium
A. calculate
B. render
C. All methods equally
D. process

Solution

  1. Step 1: Understand self time meaning

    Self time shows how much time the method itself takes, excluding calls to others.
  2. Step 2: Compare self times

    calculate has the highest self time (0.40), so it is the slowest part to optimize.
  3. Final Answer:

    calculate -> Option A
  4. Quick Check:

    Highest self time = calculate [OK]
Hint: Optimize method with highest self time first [OK]
Common Mistakes:
  • Choosing method with highest total time instead of self time
  • Thinking all methods need equal optimization
  • Ignoring self time importance
4. What is wrong with this Ruby profiling code snippet?
require 'ruby-prof'
ruby-prof.start
# some code here
result = RubyProf.stop
puts result
medium
A. Missing require statement for ruby-prof
B. Not starting the profiler before code
C. No code to profile
D. Using puts directly on result object

Solution

  1. Step 1: Check require and start

    require 'ruby-prof' and ruby-prof.start are correctly used.
  2. Step 2: Understand result object usage

    result is a RubyProf::Profile object; printing it directly with puts won't show readable output.
  3. Final Answer:

    Using puts directly on result object -> Option D
  4. Quick Check:

    Print profiling result properly, not with puts [OK]
Hint: Use printer classes to display profiling results, not puts [OK]
Common Mistakes:
  • Printing result object directly
  • Forgetting to start profiler
  • Not requiring ruby-prof gem
5. You want to profile a Ruby method heavy_task and save the results to a file in a readable format. Which code snippet correctly does this?
hard
A. require 'ruby-prof' ruby-prof.start heavy_task result = RubyProf.stop File.open('profile.txt', 'w') { |f| f.puts result }
B. require 'ruby-prof' ruby-prof.start heavy_task result = RubyProf.stop printer = RubyProf::FlatPrinter.new(result) File.open('profile.txt', 'w') { |f| printer.print(f) }
C. require 'ruby-prof' result = RubyProf.profile { heavy_task } File.write('profile.txt', result.to_s)
D. require 'ruby-prof' ruby-prof.start heavy_task result = RubyProf.stop puts result.to_s > 'profile.txt'

Solution

  1. Step 1: Understand profiling and printing

    ruby-prof returns a result object that needs a printer to format output.
  2. Step 2: Check each option for correct usage

    require 'ruby-prof' ruby-prof.start heavy_task result = RubyProf.stop printer = RubyProf::FlatPrinter.new(result) File.open('profile.txt', 'w') { |f| printer.print(f) } uses FlatPrinter to format and writes to file correctly. require 'ruby-prof' ruby-prof.start heavy_task result = RubyProf.stop File.open('profile.txt', 'w') { |f| f.puts result } writes raw object, require 'ruby-prof' result = RubyProf.profile { heavy_task } File.write('profile.txt', result.to_s) misuses to_s, require 'ruby-prof' ruby-prof.start heavy_task result = RubyProf.stop puts result.to_s > 'profile.txt' misuses puts redirection.
  3. Final Answer:

    Option B code snippet -> Option B
  4. Quick Check:

    Use printer to save profiling output [OK]
Hint: Use RubyProf printers to save readable profiling results [OK]
Common Mistakes:
  • Writing raw result object to file
  • Using puts with redirection incorrectly
  • Not using a printer to format output
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard