Bird
0
0

You want to profile only the slowest part of this code:

hard📝 Application Q8 of 15
Ruby - Ecosystem and Best Practices
You want to profile only the slowest part of this code:
require 'benchmark'
result = Benchmark.measure do
  fast = (1..1000).map { |x| x + 1 }
  slow = (1..100000).map { |x| x * 2 }
end
puts result.real.round(2)

How can you modify the code to profile only the slow part?
AUse Benchmark.measure for both maps separately and add results
BAdd sleep(1) before the slow map
CMove Benchmark.measure block to wrap only the slow map operation
DIncrease the range of the fast map to match slow
Step-by-Step Solution
Solution:
  1. Step 1: Identify profiling scope

    Profiling measures all code inside the block.
  2. Step 2: Isolate slow code

    Wrap only the slow map in Benchmark.measure to measure it alone.
  3. Final Answer:

    Move Benchmark.measure block to wrap only the slow map operation -> Option C
  4. Quick Check:

    Profile only slow code by isolating it in Benchmark block [OK]
Quick Trick: Wrap only slow code in Benchmark.measure to isolate timing [OK]
Common Mistakes:
  • Profiling entire code instead of slow part
  • Adding unrelated sleep
  • Changing fast code to match slow

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes