0
0
Rubyprogramming~15 mins

Performance profiling basics in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Performance profiling basics
📖 Scenario: You are working on a Ruby program that processes a list of numbers. You want to understand which part of your code takes the most time so you can improve it.
🎯 Goal: Learn how to use Ruby's built-in performance profiling tools to measure how long parts of your code take to run.
📋 What You'll Learn
Create an array of numbers
Set up a variable to store the start time
Write a loop that processes the numbers
Print the total time taken to process
💡 Why This Matters
🌍 Real World
Measuring how long parts of your program take helps you find slow spots and make your code faster.
💼 Career
Performance profiling is a key skill for developers to optimize applications and improve user experience.
Progress0 / 4 steps
1
Create an array of numbers
Create a variable called numbers and set it to an array containing the numbers 1 through 5.
Ruby
Need a hint?

Use square brackets [] to create an array with the numbers 1 to 5.

2
Set up start time variable
Create a variable called start_time and set it to the current time using Time.now.
Ruby
Need a hint?

Use Time.now to get the current time.

3
Process the numbers with a loop
Use a for loop with the variable num to iterate over numbers. Inside the loop, calculate the square of num and store it in a variable called square.
Ruby
Need a hint?

Use for num in numbers to loop. Multiply num by itself to get square.

4
Calculate and print elapsed time
Create a variable called end_time and set it to Time.now. Then calculate the elapsed time by subtracting start_time from end_time and store it in elapsed_time. Finally, print the text "Elapsed time: " followed by elapsed_time.
Ruby
Need a hint?

Use Time.now again for end_time. Subtract start_time from end_time. Use puts with string interpolation to print.