Measure and Benchmark Time for Ruby Methods

后端 未结 6 1033
长情又很酷
长情又很酷 2021-01-30 06:04

How can i measure the time taken by a method and the individual statements in that method in Ruby. If you see the below method i want to measure the total time taken by the meth

6条回答
  •  悲哀的现实
    2021-01-30 06:53

    The simplest way:

    require 'benchmark'
    
    def foo
     time = Benchmark.measure {
      code to test
     }
     puts time.real #or save it to logs
    end
    

    Sample output:

    2.2.3 :001 > foo
      5.230000   0.020000   5.250000 (  5.274806)
    

    Values are: cpu time, system time, total and real elapsed time.

    Source: ruby docs.

提交回复
热议问题