Challenge - 5 Problems
Performance Profiling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Benchmark.measure returns an object that holds timing data, not a simple number or string.
✗ Incorrect
Benchmark.measure returns an instance of Benchmark::Tms which contains user, system, total, and real time data.
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
GC.stat returns a collection of statistics about garbage collection.
✗ Incorrect
GC.stat returns a Hash containing various keys and values about the garbage collector's state.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
Benchmark.bm prints a formatted table with timing results for each report block.
✗ Incorrect
Benchmark.bm prints a table with a header and rows for each report label and timing data including user, system, total, and real times.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Profiling tools measure time spent in code sections.
✗ Incorrect
Benchmark.measure helps identify which code sections take the most time by measuring execution duration.
❓ Predict Output
expert2: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)
Attempts:
2 left
💡 Hint
Check if :total_allocated_objects is a valid key in GC.stat hash.
✗ Incorrect
:total_allocated_objects is a standard key in GC.stat hash, so keys.include? returns true.