0
0
Rubyprogramming~10 mins

Performance profiling basics in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to require the Ruby profiler library.

Ruby
require '[1]'
Drag options to blanks, or click blank then click option'
Aperformance
Bprofiler
Cprofile
Dbenchmark
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'profiler' instead of 'profile' causes a load error.
Using 'benchmark' is for timing, not profiling.
2fill in blank
medium

Complete the code to start profiling a block of code.

Ruby
[1] do
  sleep(1)
end
Drag options to blanks, or click blank then click option'
AProfile.profile
BProfiler.profile
CProfile do
Dprofile do
Attempts:
3 left
💡 Hint
Common Mistakes
Using Profiler instead of Profile causes an error.
Omitting .profile or using lowercase module name causes errors.
Not using 'do' to start the block causes syntax errors.
3fill in blank
hard

Fix the error in the code to correctly profile a method execution.

Ruby
require 'profile'

def slow_method
  sleep(2)
end

[1]
Drag options to blanks, or click blank then click option'
AProfile.profile { slow_method }
BProfile.profile
CProfile.profile {
DProfile.profile do
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Profile.profile slow_method without a block causes an error.
Incomplete block syntax like missing } or end causes syntax errors.
4fill in blank
hard

Fill both blanks to create a hash that stores method names and their execution times.

Ruby
times = { [1] => 1.2, [2] => 0.5 }
Drag options to blanks, or click blank then click option'
A:slow_method
B:fast_method
C:average_method
D:quick_method
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of symbols for keys.
Mixing unrelated method names and times.
5fill in blank
hard

Fill all three blanks to filter and print methods with execution time greater than 1 second.

Ruby
times.select { |[1], [2]| [2] [3] 1 }.each do |method, time|
  puts "Method: #{method}, Time: #{time}s"
end
Drag options to blanks, or click blank then click option'
Amethod
Btime
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the block variables causes wrong filtering.
Using '<' instead of '>' filters the wrong methods.