0
0
Rubyprogramming~20 mins

Performance profiling basics in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 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.