0
0
Rubyprogramming~10 mins

Performance profiling basics in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Performance profiling basics
Start Program
Run Code Section
Measure Time Taken
Record Resource Usage
Analyze Results
Identify Bottlenecks
Optimize Code
Repeat Profiling
End Program
This flow shows how performance profiling measures code execution time and resource use to find slow parts and improve them.
Execution Sample
Ruby
require 'benchmark'

result = Benchmark.measure do
  sum = 0
  1_000_000.times { |i| sum += i }
end

puts result
This Ruby code measures how long it takes to sum numbers from 0 to 999,999.
Execution Table
StepActionEvaluationResult
1Start Benchmark.measure blockBegin timingTimer started
2Initialize sumsum = 0sum is 0
3Loop 1,000,000 timesAdd i to sum each iterationsum accumulates values
4End Benchmark.measure blockStop timingTimer stopped
5Output resultPrint elapsed timeShows time taken to run loop
💡 Benchmark.measure finishes after loop completes and time is recorded
Variable Tracker
VariableStartAfter 1After 2After 3After 1,000,000Final
sum00 + 0 = 00 + 1 = 11 + 2 = 3...499999500000
Key Moments - 3 Insights
Why does Benchmark.measure wrap the code block?
Benchmark.measure starts timing before the block runs and stops after it finishes, so it measures exactly the code inside. See execution_table rows 1 and 4.
Does the sum variable affect the timing result?
Yes, because the loop does work adding to sum each time. If sum was not updated, the loop might be optimized away. See variable_tracker showing sum changes.
What does the output of Benchmark.measure show?
It shows the elapsed time and resource usage for the code block. This helps find slow parts to optimize. See execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
AThe result is printed
BThe loop runs 1,000,000 times adding i to sum
CThe timer starts
DThe sum variable is reset to zero
💡 Hint
Check execution_table row 3 describing the loop action
According to variable_tracker, what is the value of sum after 2 iterations?
A1
B3
C0
D2
💡 Hint
Look at variable_tracker row for sum after 2 iterations
If we remove the sum += i line, how would the timing change in execution_table?
ATiming would be longer because sum is not updated
BTiming would be the same
CTiming would be shorter because loop does less work
DProgram would crash
💡 Hint
Consider execution_table row 3 where sum is updated each iteration
Concept Snapshot
Performance profiling in Ruby uses Benchmark.measure to time code blocks.
Wrap the code you want to measure inside Benchmark.measure do ... end.
It records how long the code takes to run.
Use this info to find slow parts and improve them.
Repeat profiling after changes to check improvements.
Full Transcript
Performance profiling basics in Ruby involve measuring how long parts of your code take to run. Using Benchmark.measure, you wrap the code block you want to test. When the program runs, it starts a timer before the block and stops it after. This gives you the elapsed time. For example, summing numbers from 0 to 999,999 inside Benchmark.measure shows how long that loop takes. Watching the sum variable change helps understand the work done. The output tells you the time used, so you can find slow spots and make your code faster. Profiling again after changes confirms if your improvements worked.