# Fastest way to profile a method in Ruby

DevFeed: [Fastest way to profile a method in Ruby](<https://devfeed.tech/articles/fastest-way-to-profile-a-method-in-ruby-41342.md>)

Original publisher: [Read original article](<https://samsaffron.com/archive/2017/10/18/fastest-way-to-profile-a-method-in-ruby>)

Author: Sam Saffron

Published: 2017-10-18T00:21:47Z

Content type: tutorial

Language: en

Sources: [Sam Saffron](<https://devfeed.tech/sources/sam-saffron.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Instrumentation](<https://devfeed.tech/topics/instrumentation.md>), [Benchmark](<https://devfeed.tech/topics/benchmark.md>), [multithreading](<https://devfeed.tech/topics/multithreading.md>), [Process](<https://devfeed.tech/topics/process.md>), [telemetry](<https://devfeed.tech/topics/telemetry.md>)

Tags: [benchmark](<https://devfeed.tech/tags/benchmark.md>), [instrumentation](<https://devfeed.tech/tags/instrumentation.md>), [multithreading](<https://devfeed.tech/tags/multithreading.md>), [profile](<https://devfeed.tech/tags/profile.md>), [redis](<https://devfeed.tech/tags/redis.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [sql](<https://devfeed.tech/tags/sql.md>)

## AI overview

This Ruby tutorial compares methods for measuring elapsed time and patching methods for instrumentation. It finds that Process.clock_gettime with CLOCK_MONOTONIC is faster and more accurate than using Time.now in the presented benchmarks, and discusses alternatives for instrumenting code without source-level changes.

## Source excerpt

Lately I needed to add some instruments into Discourse. I wanted every request to measure: How many SQL statements and Redis commands are executed? How much time was spent in those sections? This can easily be abstracted to a more generalized problem of class ExternalClass def method_to_measure end end Solving this problem breaks down into a few particular sub-problems What is the fastest way of measuring elapsed time in a method? What is the fastest patch to a method that can add instrumentation? How does one account for multithreading? Fastest way to measure a method The first and simplest problem to solve is pretending we do not have to patch anything. If we had access to all the source and could add all the instruments we wanted to, what is the quickest way of measuring elapsed time? There are two techniques we can use in Ruby. Create a Time object before and after and delta. Use Process.clock_gettime to return a Float before and after and delta. Let's measure: require 'benchmark/ips' class Test attr_reader :count, :duration def initialize @count = 0 @duration = 0.0 end def work end def method work end def time_method @count += 1 t = Time.now work ensure @duration += Time.now - t end def process_clock_get_time_method @count += 1 t = Process.clock_gettime(Process::CLOCK_MONOTONIC) work ensure @duration += Process.clock_gettime(Process::CLOCK_MONOTONIC) - t end end t = Test.new Benchmark.ips do |b| b.report "method" do |times| i = 0 while i < times t.method i += 1 end end b.report "time_method" do |times| i = 0 while i < times t.time_method i += 1 end end b.report "process_clock_get_time_method" do |times| i = 0 while i < times t.process_clock_get_time_method i += 1 end end end # Calculating ------------------------------------- # method 19.623M (± 3.5%) i/s - 98.227M in 5.012204s # time_method 1.596M (± 1.2%) i/s - 8.061M in 5.050321s # process_clock_get_time_method # 4.972M (± 1.7%) i/s - 24.908M in 5.011634s As expected Process.clock_gettime(Process::CLOCK_MONO