How to Benchmark Your Ruby Code for Performance Optimization

Saleh Salem
3 min readFeb 21, 2023

--

Benchmark

How to Benchmark Your Ruby Code for Performance Optimization

Benchmarking is measuring the performance of a program or a specific piece of code to identify potential bottlenecks and optimize its execution time.

Ruby has a benchmarking tool called Benchmark in its standard library.
You can use the built-in Benchmark module to easily measure the running time of your code.

Here’s an example of how to use the Benchmark module to measure the time it takes to sort an array of integers:

require 'benchmark'

array = (1..100000).map { rand }

time = Benchmark.realtime do
array.sort
end

puts "Sorting the array took #{time} seconds."

PC1 => Sorting the array took 0.020800999831408262 seconds.
PC2 => Sorting the array took 0.03456465480849147 seconds.

NOTE: Result will be different in other pc.

In this example, we first generate an array of 100000 random integers using the map method. Then, we use the Benchmark.realtime method to measure the time it takes to sort the array using the built-in sort method.
Finally, we print the elapsed time in seconds using puts.

Another Example

Comparing the performance of different Code:

This code will get employees using two different ways.

require 'benchmark'

Benchmark.bm do |x|
x.report("Code 1") do
Employee.where(organization_id: 5, status: :active)
end

x.report("Code 2") do
organization = Organization.find(5)
organization.employees.active
end
end

Explain the difference between two code:

The first one will retrieve employee records from the database based on the specified conditions whose organization_id column is 5 and whose status column is active. The result is an array of Employee objects that match these conditions.

The second one retrieves an Organization object from the database with an ID of 5 using the find method and then calls the employees who belong to that organization, which returns a collection of all the employees where status is active which returns a collection that includes only the employees who have an “active” status

Another Example of memory usage:

The benchmark-memory gem needs to be installed before using the total_allocated_object method.

require 'benchmark'

memory = Benchmark.measure do
Employee.where(organization_id: 5, status: :active)
end

puts "Memory usage: #{memory.total_allocated_object} bytes"

In this example, I am using the Benchmark. measure method to measure the memory usage of the query.
I assign the result of the measure method to a variable called memory.
We can then print the total allocated memory in bytes using the total_allocated_object attribute of the memory variable.

References:

You can go deep into references and Performance Optimization with your code.

Thanks

Follow ME

Linked In - GitHub

--

--

Saleh Salem

Software Engineering | Ruby On Rails | Databases | Git | AWS | Docker | Kubernetes | Redis | Newrelic || Payment Subscriptions | PlanHat | Chargebee and more